Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Last active March 21, 2019 14:53
Show Gist options
  • Select an option

  • Save rosskevin/a43e254bbf88b877d36e9e989d6473ea to your computer and use it in GitHub Desktop.

Select an option

Save rosskevin/a43e254bbf88b877d36e9e989d6473ea to your computer and use it in GitHub Desktop.
import * as pulumi from '@pulumi/pulumi'
import * as gcp from '@pulumi/gcp'
export function createSecret(key: gcp.serviceAccount.Key): pulumi.Output<string> {
return key.privateKey.apply(key =>
JSON.parse(Buffer.from(key, 'base64').toString('ascii')),
) as any
}
import { ComponentResource, ComponentResourceOptions, Output } from '@pulumi/pulumi'
import kebabCase from 'lodash/kebabCase'
import * as gcp from '@pulumi/gcp'
import { upperCaseFirstLetter } from '@alienfast/common'
import { getStack } from '@pulumi/pulumi/runtime'
import { createSecret } from './createSecret'
import { Log } from '@alienfast/common-node'
import camelCase from 'lodash/camelCase'
const log = new Log('Identity', 'debug')
export interface IdentityArgs {
readonly iamRoles?: string[]
readonly project: string
}
/**
*
* @see https://cloud.google.com/iam/docs/understanding-roles
* @see https://cloud.google.com/kubernetes-engine/docs/how-to/iam
*/
export class Identity extends ComponentResource {
public account!: gcp.serviceAccount.Account
public key!: gcp.serviceAccount.Key
public secret!: Output<string>
constructor(nameArg: string, args: IdentityArgs, opts: ComponentResourceOptions) {
const name = `${nameArg}${upperCaseFirstLetter(getStack() || '')}`
super('af:pulumi:Identity', name, {}, opts)
const { iamRoles, project } = args
log.debug('creating service account:', name)
this.account = new gcp.serviceAccount.Account(
name,
{
project,
accountId: kebabCase(name),
},
{ parent: this },
)
if (iamRoles) {
for (let role of iamRoles) {
let bindingName = camelCase(role.replace('roles/', ''))
bindingName = `${name}${upperCaseFirstLetter(bindingName)}`
log.debug('creating role binding:', bindingName, '=>', role)
new gcp.projects.IAMBinding(
bindingName,
{
project,
role,
members: [this.account.email.apply(email => `serviceAccount:${email}`)],
},
{ parent: this, dependsOn: [this.account] },
)
}
}
this.key = new gcp.serviceAccount.Key(
`${name}Key`,
{ serviceAccountId: this.account.name },
{ parent: this },
)
// Export client secret so that CI/CD systems can authenticate as this service account.
this.secret = createSecret(this.key)
this.registerOutputs({
account: this.account,
key: this.key,
secret: this.secret,
})
}
}
import { project } from './gcp'
import { Identity } from '@alienfast/pulumi'
import { protect } from './protect'
export const ciInf = new Identity(
'ciInf',
{
project,
iamRoles: [
'roles/container.admin', // Full access to clusters and their Kubernetes resources. Necessary to create ClusterRoleBindings.
'roles/cloudsql.admin',
'roles/storage.admin',
],
},
{ protect },
)
export const ciInfSecret = ciInf.secret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment