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

Ingest the remaining entities and relationships #5

Merged
merged 5 commits into from
Sep 10, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 58 additions & 53 deletions src/steps/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { match } from 'node-match-path';
import { createAPIClient } from '../client';
import { IntegrationConfig, ArtifactoryPermission } from '../types';
import { getUserKey, getGroupKey } from './access';
import { getRepositoryKey } from './repositories';
import { entities, relationships } from '../constants';

type PermissionRules = {
Expand Down Expand Up @@ -84,15 +83,53 @@ function constructPermissionsMap(
return targetPermissionsMap;
}

async function createPermissionRelationship(
jobState: JobState,
permission: ArtifactoryPermission,
permissionEntity: Entity,
relationshipType: 'build' | 'repo',
targetType: string,
relationshipTemplate: {
_type: string;
_class: RelationshipClass;
},
keyGenerator: (entity: Entity) => string,
): Promise<void> {
const includes = permission[relationshipType]?.['include-patterns'] || [];
const excludes = permission[relationshipType]?.['exclude-patterns'] || [];

const matchesIncludes = (target: string) =>
includes.some((pattern) => match(pattern.split('/')[0], target).matches);
const matchesExcludes = (target: string) =>
excludes.some((pattern) => match(pattern.split('/')[0], target).matches);

await jobState.iterateEntities({ _type: targetType }, async (entity) => {
const [, target] = entity._key.split(':');

if (matchesIncludes(target) && !matchesExcludes(target)) {
await jobState.addRelationship(
createMappedRelationship({
...relationshipTemplate,
_key: keyGenerator(entity),
source: permissionEntity,
target: entity,
}),
);
}
});
}

async function createPermissionBuildAllowsRelationships(
jobState: JobState,
permission: ArtifactoryPermission,
permissionEntity: Entity,
): Promise<void> {
return createPermissionBuildRelationship(
return createPermissionRelationship(
jobState,
permission,
permissionEntity,
'build',
entities.BUILD._type,
{
_class: RelationshipClass.ALLOWS,
_type: relationships.PERMISSION_ALLOWS_BUILD._type,
Expand All @@ -101,40 +138,22 @@ async function createPermissionBuildAllowsRelationships(
);
}

async function createPermissionBuildRelationship(
async function createPermissionRepositoryAllowsRelationships(
jobState: JobState,
permission: ArtifactoryPermission,
permissionEntity: Entity,
relationshipTemplate: {
_type: string;
_class: RelationshipClass;
},
keyGenerator: (buildEntity: Entity) => string,
): Promise<void> {
const includes = permission.build?.['include-patterns'] || [];
const excludes = permission.build?.['exclude-patterns'] || [];

const matchesIncludes = (target: string) =>
includes.some((pattern) => match(pattern.split('/')[0], target).matches);
const matchesExcludes = (target: string) =>
excludes.some((pattern) => match(pattern.split('/')[0], target).matches);

await jobState.iterateEntities(
{ _type: entities.BUILD._type },
async (buildEntity) => {
const [, buildName] = buildEntity._key.split(':');

if (matchesIncludes(buildName) && !matchesExcludes(buildName)) {
await jobState.addRelationship(
createMappedRelationship({
...relationshipTemplate,
_key: keyGenerator(buildEntity),
source: permissionEntity,
target: buildEntity,
}),
);
}
return createPermissionRelationship(
jobState,
permission,
permissionEntity,
'repo',
entities.REPOSITORY._type,
{
_class: RelationshipClass.ALLOWS,
_type: relationships.PERMISSION_ALLOWS_REPOSITORY._type,
},
(buildEntity) => `${permissionEntity._key}|allows|${buildEntity._key}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal since it's functionally correct, but here it should be permissionEntity.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean repositoryEntity? In any case, I renamed it since it was bothering me too much 🙂.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thanks.

);
}

Expand Down Expand Up @@ -162,14 +181,14 @@ export async function fetchPermissions({
for (const [groupName, { permissions }] of Object.entries(
constructPermissionsMap(permission, 'groups'),
)) {
const userEntity = await jobState.findEntity(getGroupKey(groupName));
const groupEntity = await jobState.findEntity(getGroupKey(groupName));

if (userEntity) {
if (groupEntity) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.ASSIGNED,
from: permissionEntity,
to: userEntity,
to: groupEntity,
properties: permissions,
}),
);
Expand All @@ -194,25 +213,11 @@ export async function fetchPermissions({
}
}

const targetRepositories = permission.repo?.repositories || [];

for (const targetRepository of targetRepositories) {
const repositoryEntity = await jobState.findEntity(
getRepositoryKey(targetRepository),
);

if (repositoryEntity) {
await jobState.addRelationship(
createMappedRelationship({
_class: RelationshipClass.ALLOWS,
_type: relationships.PERMISSION_ALLOWS_REPOSITORY._type,
_key: `${permissionEntity._key}|allows|${repositoryEntity._key}`,
source: permissionEntity,
target: repositoryEntity,
}),
);
}
}
await createPermissionRepositoryAllowsRelationships(
jobState,
permission,
permissionEntity,
);

await createPermissionBuildAllowsRelationships(
jobState,
Expand Down