Skip to content

Commit

Permalink
fix: added extra logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Aug 9, 2023
1 parent ca223cc commit 8cdd1c1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
11 changes: 7 additions & 4 deletions packages/aws/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AccessKey, CreateAccessKeyCommand, DeleteAccessKeyCommand, IAMClient } from '@aws-sdk/client-iam';
import { SourceModule, KeyInfo, getEnv, prefix } from '@refreshly/core';
import * as assert from 'assert';
import { SourceModule, KeyInfo, getEnv, prefix, Logger, PartiallyRequired } from '@refreshly/core';

class AWSSourceModule extends SourceModule {
protected declare options: AWSSourceModule.Options;
protected declare options: PartiallyRequired<AWSSourceModule.Options, 'key' | 'secretKey'>;

private accessKey?: AccessKey;

constructor({ targets, key, secretKey, ...options }: AWSSourceModule.Options) {
Expand Down Expand Up @@ -83,7 +83,10 @@ class AWSSourceModule extends SourceModule {
}

async cleanup(): Promise<void> {
assert.ok(this.accessKey);
if (!this.accessKey) {
Logger.info(`(${this.name}) No cleanup necessary, skipping...`);
return;
}

// Retain the new key and cleanup the old key
const client = new IAMClient({
Expand Down
1 change: 1 addition & 0 deletions packages/core/@types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './key-info';
export * from './source-module';
export * from './target-module';
export * from './required';
1 change: 1 addition & 0 deletions packages/core/@types/required.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PartiallyRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
8 changes: 1 addition & 7 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ export async function Refreshly(...sources: SourceModule[]) {
}
}

export namespace Refreshly {
export function setLogLevel(level: LogLevel): void {
Logger.setLevel(level);
}
}

// This is temporary
export function getEnv<T>(configKey: string, configValue?: T, ...keys: string[]): T {
if (configValue) return configValue;
Expand All @@ -44,4 +38,4 @@ export function prefix(...values: string[]): string {
}

export * from './@types';
export { LogLevel };
export { LogLevel, Logger };
42 changes: 21 additions & 21 deletions packages/github/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { Octokit } from 'octokit';
import { KeyInfo, ITargetModule, getEnv, prefix } from '@refreshly/core';
import { KeyInfo, ITargetModule, getEnv, prefix, PartiallyRequired } from '@refreshly/core';
import { getEncryptedValueForGitHub } from './utils/sodium';

class GitHubTargetModule implements ITargetModule {
#options: GitHubTargetModule.Options;
#octokit: Octokit;
#publicKey: Promise<{
private options: PartiallyRequired<GitHubTargetModule.Options, 'token'>;
private octokit: Octokit;
private publicKey: Promise<{
key_id: string;
key: string;
}>;

constructor({ token, ...options }: GitHubTargetModule.Options) {
this.#options = {
this.options = {
...options,
token: getEnv('token', token, 'GITHUB_TOKEN', 'GH_TOKEN'),
};

this.#octokit = new Octokit({
auth: this.#options.token,
this.octokit = new Octokit({
auth: this.options.token,
});

// Run this in the background to try to ensure its cached by the time we need it
this.#getOrgPublicKey(this.#options.org);
this.getOrgPublicKey(this.options.org);
}

get name(): string {
return 'github';
}

async #getOrgPublicKey(org: string): Promise<{ key_id: string; key: string }> {
if (!this.#publicKey) {
this.#publicKey = this.#octokit.rest.users.getAuthenticated().then(async () => {
const { data } = await this.#octokit.rest.actions.getOrgPublicKey({
private async getOrgPublicKey(org: string): Promise<{ key_id: string; key: string }> {
if (!this.publicKey) {
this.publicKey = this.octokit.rest.users.getAuthenticated().then(async () => {
const { data } = await this.octokit.rest.actions.getOrgPublicKey({
org,
});

Expand All @@ -42,29 +42,29 @@ class GitHubTargetModule implements ITargetModule {
});
}

return this.#publicKey;
return this.publicKey;
}

async #update(keyInfos: KeyInfo[]): Promise<void> {
await this.#octokit.rest.users.getAuthenticated();
private async update(keyInfos: KeyInfo[]): Promise<void> {
await this.octokit.rest.users.getAuthenticated();

const { key, key_id } = await this.#getOrgPublicKey(this.#options.org);
const { key, key_id } = await this.getOrgPublicKey(this.options.org);

await Promise.all(
keyInfos.map(async (keyInfo) => {
await this.#octokit.rest.actions.createOrUpdateOrgSecret({
await this.octokit.rest.actions.createOrUpdateOrgSecret({
key_id,
org: this.#options.org,
secret_name: prefix(this.#options.prefix, keyInfo.name),
org: this.options.org,
secret_name: prefix(this.options.prefix, keyInfo.name),
visibility: 'all',
encrypted_value: await getEncryptedValueForGitHub(key, keyInfo.value),
});
})
);
}

revert = this.#update;
target = this.#update;
revert = this.update;
target = this.update;
}

namespace GitHubTargetModule {
Expand Down

0 comments on commit 8cdd1c1

Please sign in to comment.