Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow extra data to be saved to secret #74

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions packages/probot-kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Additionally this library offers CRUD methods for the secret management allowing

- `createTokenSecret(context)`: Promise which creates the token secret.
- `getTokenSecretName(context)`: Returns secret name relevant to current context.
- `readTokenSecret(context)`: Promise which resolves to the token secret content.
- `updateTokenSecret(context)`: Promise which updates the token secret.
- `deleteTokenSecret(context)`: Promise which deletes the token secret.

Expand Down Expand Up @@ -109,3 +110,28 @@ module.exports = app => {
});
};
```

### Store additional data per installation

This extension allows you to store additional data for each installation into the token `Secret` object.

```js

const kubernetes = require('@operate-first/probot-kubernetes');

module.exports = app => {
app.on('installation.created', async (context) => {
await kubernetes.createTokenSecret(
context,
{
installationDetails: Json.stringify(context.payload),
}
);
});

app.on('push', (context) => {
const secret = await kubernetes.readTokenSecret(context);
app.log.info(secret.spec.stringData?.installationDetails)
});
};
```
31 changes: 26 additions & 5 deletions packages/probot-kubernetes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const k8sNamespace = (() => {
})();

type ApiConstructor<T extends k8s.ApiType> = new (server: string) => T;

export const useApi = <T extends k8s.ApiType>(
apiClientType: ApiConstructor<T>
): T => kc.makeApiClient(apiClientType);
Expand Down Expand Up @@ -56,7 +57,10 @@ const unpackExceptionMessage = (err: any) => {
throw err?.body?.message || err;
};

const createSecretPayload = async (context: any) => {
const createSecretPayload = async (
context: any,
extraData?: Record<string, string>
) => {
const appAuth = (await context.octokit.auth({
type: 'installation',
})) as InstallationAccessTokenAuthentication;
Expand All @@ -77,18 +81,32 @@ const createSecretPayload = async (context: any) => {
},
},
stringData: {
...extraData,
token: appAuth.token,
orgName: orgName,
},
} as k8s.V1Secret;
};

export const createTokenSecret = async (context: any) => {
export const createTokenSecret = async (
context: any,
extraData?: Record<string, string>
) => {
return useApi(k8s.CoreV1Api)
.createNamespacedSecret(getNamespace(), await createSecretPayload(context))
.createNamespacedSecret(
getNamespace(),
await createSecretPayload(context, extraData)
)
.catch(unpackExceptionMessage);
};

export const readTokenSecret = (context: any) => {
return useApi(k8s.CoreV1Api)
.readNamespacedSecret(getTokenSecretName(context), getNamespace())
.catch(unpackExceptionMessage)
.then((r) => r.body);
};

export const deleteTokenSecret = async (context: any) => {
return useApi(k8s.CoreV1Api)
.deleteNamespacedSecret(
Expand All @@ -98,7 +116,10 @@ export const deleteTokenSecret = async (context: any) => {
.catch(unpackExceptionMessage);
};

export const updateTokenSecret = async (context: any) => {
export const updateTokenSecret = async (
context: any,
extraData?: Record<string, string>
) => {
const appSecret = await useApi(k8s.CoreV1Api)
.readNamespacedSecret(
SECRET_NAME_PREFIX + context.payload.installation.id,
Expand All @@ -119,7 +140,7 @@ export const updateTokenSecret = async (context: any) => {
.patchNamespacedSecret(
SECRET_NAME_PREFIX + context.payload.installation.id,
getNamespace(),
await createSecretPayload(context),
await createSecretPayload(context, extraData),
undefined,
undefined,
undefined,
Expand Down