Skip to content

Commit

Permalink
Merge pull request #78 from bosch-io/feature/policy-migration-updates
Browse files Browse the repository at this point in the history
Feature/policy migration updates
  • Loading branch information
thjaeckle authored Nov 30, 2023
2 parents 82451ac + 43f0557 commit 642ffdc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
26 changes: 25 additions & 1 deletion policy-migration/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type LogLevel = (
export enum Migration {
ReplaceSubject = "replaceSubject",
AddSubject = "addSubject",
AddEntry = "addEntry"
AddEntry = "addEntry",
ReplaceEntries = "replaceEntries"
}

export type Config = {
Expand All @@ -37,10 +38,17 @@ export type Config = {
readonly secret: string;
readonly scope: string;
};

readonly basicAuth: {
readonly username: string;
readonly password: string;
};

readonly apiKey: {
readonly key: string;
readonly value: string
};

readonly namespaces?: [string];
readonly filter?: string;
readonly pageSize: number;
Expand All @@ -58,6 +66,22 @@ export type Config = {
};

readonly migrations: [{ [key: string]: unknown }];

readonly policyEntries: {
[label: string]: {
subjects: {
[subject: string]: {
type: string;
};
};
resources: {
[resource: string]: {
grant: (string)[];
revoke: (string)[];
};
};
};
}
};

const defaults = {
Expand Down
13 changes: 9 additions & 4 deletions policy-migration/src/http/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ export class HttpAuth {
headers.append(
"Authorization",
"Basic " +
btoa(
this.cfg.basicAuth.username + ":" +
this.cfg.basicAuth.password,
),
btoa(
this.cfg.basicAuth.username + ":" +
this.cfg.basicAuth.password,
),
);
} else if (this.cfg.apiKey) {
headers.append(
this.cfg.apiKey.key,
this.cfg.apiKey.value
);
}
}
Expand Down
9 changes: 8 additions & 1 deletion policy-migration/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { HttpErrorResponse, MigrationResult, Progress } from "./model/base.ts";
import { Policy } from "./model/policy.ts";
import { Search } from "./search.ts";
import { HttpAuth } from "./http/auth.ts";
import { AddEntry, AddSubject, ReplaceSubject } from "./model/migration.ts";
import { AddEntry, AddSubject, ReplaceEntries, ReplaceSubject } from "./model/migration.ts";

/**
* The policy migration is done in several steps:
Expand Down Expand Up @@ -222,6 +222,8 @@ export class MigrationStep {
return this.addSubject(policy, step as AddSubject);
case Migration.AddEntry:
return this.addEntry(policy, step as AddEntry);
case Migration.ReplaceEntries:
return this.replaceEntries(policy, step as ReplaceEntries)
default:
this.logger.info(`Unknown migration ${label}. Ignoring.`);
return false;
Expand Down Expand Up @@ -263,4 +265,9 @@ export class MigrationStep {
}
return changed;
}

private replaceEntries(policy: Policy, replaceEntries: ReplaceEntries) {
policy.entries = replaceEntries.policyEntries;
return true;
}
}
6 changes: 5 additions & 1 deletion policy-migration/src/model/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export type AddEntry = {
label: string;
entry: PolicyEntry;
replace: boolean;
};
};

export type ReplaceEntries = {
policyEntries: { [label: string]: PolicyEntry };
}
9 changes: 8 additions & 1 deletion policy-migration/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ export class Search {
this.completed = true;
}

const policiesMap: Map<String, Policy> = new Map<String, Policy>();
sr.items.forEach((item) => {
policiesMap.set(item._policy.policyId, item._policy);
});
const policies: Policy[] = [];
sr.items.forEach((item) => policies.push(item._policy));
policiesMap.forEach((value, _) => {
policies.push(value);
});

return policies;
});
}
Expand Down

0 comments on commit 642ffdc

Please sign in to comment.