Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #80 from JupiterOne/INT-11063
Browse files Browse the repository at this point in the history
use on-disk dkt; remove findEntity calls
  • Loading branch information
RonaldEAM authored May 20, 2024
2 parents 6006c39 + 22ad148 commit e84e53b
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 59 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM node:18-alpine

ENV JUPITERONE_INTEGRATION_DIR=/opt/jupiterone/integration
ENV USE_ON_DISK_DKT='1'
ENV NO_COLLECTION_METRICS='1'

COPY package.json yarn.lock tsconfig.json LICENSE ${JUPITERONE_INTEGRATION_DIR}/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@jupiterone/integration-sdk-http-client": "^12.8.1",
"lmdb": "^3.0.8",
"node-fetch": "^2.7.0",
"node-match-path": "^0.4.4"
}
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export const entities: Record<EntityConstantKeys, StepEntityMetadata> = {
resourceName: 'ArtifactCodeModule',
_type: 'artifactory_artifact_codemodule',
_class: ['CodeModule'],
indexMetadata: {
enabled: false,
},
},
BUILD: {
resourceName: 'Build',
Expand Down
20 changes: 12 additions & 8 deletions src/steps/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ export async function fetchUsers({

if (user.groups) {
for (const groupName of user.groups) {
const groupEntity = await jobState.findEntity(getGroupKey(groupName));
if (groupEntity) {
const groupKey = getGroupKey(groupName);
if (jobState.hasKey(groupKey)) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.HAS,
from: groupEntity,
to: userEntity,
fromKey: groupKey,
fromType: entities.GROUP._type,
toKey: userEntity._key,
toType: entities.USER._type,
}),
);
}
Expand Down Expand Up @@ -160,16 +162,18 @@ export async function fetchAccessTokens({

const [, , username] = token.subject.split('/');

const userEntity = await jobState.findEntity(getUserKey(username));
const userKey = getUserKey(username);

if (userEntity) {
if (jobState.hasKey(userKey)) {
await Promise.all([
jobState.addEntity(tokenEntity),
jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.ASSIGNED,
from: tokenEntity,
to: userEntity,
fromKey: tokenEntity._key,
fromType: entities.ACCESS_TOKEN._type,
toKey: userKey,
toType: entities.USER._type,
}),
),
]);
Expand Down
30 changes: 12 additions & 18 deletions src/steps/builds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
createIntegrationEntity,
IntegrationStep,
createDirectRelationship,
JobState,
Entity,
RelationshipClass,
} from '@jupiterone/integration-sdk-core';

Expand All @@ -17,11 +15,8 @@ export function getBuildKey(name: string): string {
return `artifactory_build:${name}`;
}

async function createBuildEntity(
jobState: JobState,
build: ArtifactoryBuild,
): Promise<Entity> {
const buildEntity = createIntegrationEntity({
function createBuildEntity(build: ArtifactoryBuild) {
return createIntegrationEntity({
entityData: {
source: build,
assign: {
Expand All @@ -33,8 +28,6 @@ async function createBuildEntity(
},
},
});

return jobState.addEntity(buildEntity);
}

export async function fetchBuilds({
Expand All @@ -46,21 +39,22 @@ export async function fetchBuilds({

try {
await apiClient.iterateBuilds(async (build) => {
const buildEntity =
(await jobState.findEntity(getBuildKey(build.name))) ||
(await createBuildEntity(jobState, build));
const buildEntity = createBuildEntity(build);
if (!jobState.hasKey(buildEntity._key)) {
await jobState.addEntity(buildEntity);
}

for (const artifactUri of build.artifacts) {
const artifactEntity = await jobState.findEntity(
getArtifactKey(artifactUri),
);
const artifactKey = getArtifactKey(artifactUri);

if (artifactEntity) {
if (jobState.hasKey(artifactKey)) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.CREATED,
from: buildEntity,
to: artifactEntity,
fromKey: buildEntity._key,
fromType: entities.BUILD._type,
toKey: artifactKey,
toType: entities.ARTIFACT_CODEMODULE._type,
}),
);
}
Expand Down
34 changes: 19 additions & 15 deletions src/steps/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ async function createPermissionAllowsRepositoryGroupRelationships(
continue;
}

const group = await jobState.findEntity(
getRepositoryGroupKey(repositoryName),
);
const groupKey = getRepositoryGroupKey(repositoryName);

if (group) {
if (jobState.hasKey(groupKey)) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.ALLOWS,
from: permissionEntity,
to: group,
fromKey: permissionEntity._key,
fromType: entities.PERMISSION._type,
toKey: groupKey,
toType: entities.REPOSITORY_GROUP._type,
properties: {
_type: relationships.PERMISSION_ALLOWS_REPOSITORY_GROUP._type,
_key: `${permissionEntity._key}|allows|${group._key}`,
_key: `${permissionEntity._key}|allows|${groupKey}`,
},
}),
);
Expand Down Expand Up @@ -195,14 +195,16 @@ export async function fetchPermissions({
for (const [groupName, permissions] of Object.entries(
constructPermissionsMap(permission, 'groups'),
)) {
const groupEntity = await jobState.findEntity(getGroupKey(groupName));
const groupKey = getGroupKey(groupName);

if (groupEntity) {
if (jobState.hasKey(groupKey)) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.ASSIGNED,
from: permissionEntity,
to: groupEntity,
fromKey: permissionEntity._key,
fromType: entities.PERMISSION._type,
toKey: groupKey,
toType: entities.GROUP._type,
properties: permissions,
}),
);
Expand All @@ -213,14 +215,16 @@ export async function fetchPermissions({
for (const [username, permissions] of Object.entries(
constructPermissionsMap(permission, 'users'),
)) {
const userEntity = await jobState.findEntity(getUserKey(username));
const userKey = getUserKey(username);

if (userEntity) {
if (jobState.hasKey(userKey)) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.ASSIGNED,
from: permissionEntity,
to: userEntity,
fromKey: permissionEntity._key,
fromType: entities.PERMISSION._type,
toKey: userKey,
toType: entities.USER._type,
properties: permissions,
}),
);
Expand Down
25 changes: 7 additions & 18 deletions src/steps/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
Steps,
} from '../../constants';
import { IntegrationConfig } from '../../types';
import { compareEntities } from '../../utils';
import { createArtifactEntity, getArtifactKey } from './converters';
import { createArtifactEntity } from './converters';

export function getRepositoryKey(name: string): string {
return `artifactory_repository:${name}`;
Expand Down Expand Up @@ -139,28 +138,18 @@ export async function fetchArtifacts({
await apiClient.iterateRepositoryArtifacts(
repoKey,
async (artifact) => {
const artifactKey = getArtifactKey(artifact.uri);
let artifactEntity = await jobState.findEntity(artifactKey);
const artifactEntity = createArtifactEntity(artifact, packageType);

if (!artifactEntity) {
artifactEntity = createArtifactEntity(artifact, packageType);
if (!jobState.hasKey(artifactEntity._key)) {
await jobState.addEntity(artifactEntity);
} else {
const duplicatedArtifact = createArtifactEntity(
artifact,
packageType,
);
const duplicateEntityReport = compareEntities(
artifactEntity,
duplicatedArtifact,
);
logger.debug(duplicateEntityReport, 'Duplicate entity report.');
}

const repoArtifactRelationship = createDirectRelationship({
_class: RelationshipClass.HAS,
from: repositoryEntity,
to: artifactEntity,
fromKey: repositoryEntity._key,
fromType: entities.REPOSITORY._type,
toKey: artifactEntity._key,
toType: entities.ARTIFACT_CODEMODULE._type,
});

if (!jobState.hasKey(repoArtifactRelationship._key)) {
Expand Down
Loading

0 comments on commit e84e53b

Please sign in to comment.