|
| 1 | +import { apiBase, IncidentCommander } from "../axios"; |
| 2 | +import { RoleBindingDB } from "../types/roleBindings"; |
| 3 | +import { TokenRole, TokenRoleBinding } from "./tokens"; |
| 4 | + |
| 5 | +type PersonInfo = { |
| 6 | + id: string; |
| 7 | + name: string; |
| 8 | + email?: string; |
| 9 | + avatar?: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export type ServiceAccount = { |
| 13 | + id: string; |
| 14 | + name: string; |
| 15 | + created_at: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export type ServiceAccountDetail = ServiceAccount & { |
| 19 | + /** What it holds of kubernetes-proxy:read and kubernetes-proxy:write */ |
| 20 | + kubernetes_proxy?: Record<string, boolean>; |
| 21 | + /** Omitted unless the caller may read RoleBindings */ |
| 22 | + role_bindings?: RoleBindingDB[]; |
| 23 | +}; |
| 24 | + |
| 25 | +export type ServiceAccountToken = { |
| 26 | + id: string; |
| 27 | + name: string; |
| 28 | + created_at: string; |
| 29 | + expires_at?: string | null; |
| 30 | + auto_renew?: boolean; |
| 31 | + /** Whoever issued the token */ |
| 32 | + issuer?: PersonInfo | null; |
| 33 | + role_bindings?: TokenRoleBinding[]; |
| 34 | +}; |
| 35 | + |
| 36 | +export type CreateServiceAccountTokenRequest = { |
| 37 | + name: string; |
| 38 | + expiry: string; |
| 39 | + autoRenew: boolean; |
| 40 | + roles: TokenRole[]; |
| 41 | +}; |
| 42 | + |
| 43 | +export type CreatedServiceAccountToken = { |
| 44 | + id: string; |
| 45 | + name: string; |
| 46 | + token: string; |
| 47 | + expires_at?: string; |
| 48 | +}; |
| 49 | + |
| 50 | +// Every route takes service-account:manage, and is refused to a request made with an access token |
| 51 | +export async function getServiceAccounts() { |
| 52 | + const { data } = await apiBase.get<ServiceAccount[]>("/service-accounts"); |
| 53 | + return data ?? []; |
| 54 | +} |
| 55 | + |
| 56 | +export async function getServiceAccount(name: string) { |
| 57 | + const { data } = await apiBase.get<ServiceAccountDetail>( |
| 58 | + `/service-accounts/${encodeURIComponent(name)}` |
| 59 | + ); |
| 60 | + return data; |
| 61 | +} |
| 62 | + |
| 63 | +export async function createServiceAccount(name: string) { |
| 64 | + const { data } = await apiBase.post<ServiceAccount>("/service-accounts", { |
| 65 | + name |
| 66 | + }); |
| 67 | + return data; |
| 68 | +} |
| 69 | + |
| 70 | +export async function deleteServiceAccount(name: string) { |
| 71 | + await apiBase.delete(`/service-accounts/${encodeURIComponent(name)}`); |
| 72 | +} |
| 73 | + |
| 74 | +export async function getServiceAccountTokens(name: string) { |
| 75 | + const { data } = await apiBase.get<ServiceAccountToken[]>( |
| 76 | + `/service-accounts/${encodeURIComponent(name)}/tokens` |
| 77 | + ); |
| 78 | + return data ?? []; |
| 79 | +} |
| 80 | + |
| 81 | +export async function createServiceAccountToken( |
| 82 | + name: string, |
| 83 | + request: CreateServiceAccountTokenRequest |
| 84 | +) { |
| 85 | + const { data } = await apiBase.post<CreatedServiceAccountToken>( |
| 86 | + `/service-accounts/${encodeURIComponent(name)}/tokens`, |
| 87 | + request |
| 88 | + ); |
| 89 | + return data; |
| 90 | +} |
| 91 | + |
| 92 | +export async function revokeServiceAccountToken(name: string, id: string) { |
| 93 | + await apiBase.delete( |
| 94 | + `/service-accounts/${encodeURIComponent(name)}/tokens/${id}` |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +/** Issues a new token of the service account, bound to the kubernetes-proxy Role, and returns a kubeconfig that uses it. */ |
| 99 | +export async function downloadServiceAccountKubeconfig( |
| 100 | + serviceAccount: string, |
| 101 | + options: { name?: string; expiry?: string; autoRenew?: boolean } |
| 102 | +) { |
| 103 | + const { data } = await apiBase.get<string>("/kubeconfig", { |
| 104 | + params: { |
| 105 | + serviceAccount, |
| 106 | + ...(options.name ? { name: options.name } : {}), |
| 107 | + ...(options.expiry ? { expiry: options.expiry } : {}), |
| 108 | + ...(options.autoRenew ? { autoRenew: true } : {}) |
| 109 | + } |
| 110 | + // The kubeconfig is YAML, which the default JSON parsing leaves as text, while an error's JSON body is still parsed |
| 111 | + }); |
| 112 | + return data; |
| 113 | +} |
| 114 | + |
| 115 | +/** Names of service accounts, which anyone with read on person may see. Used to pick RoleBinding subjects. */ |
| 116 | +export async function getServiceAccountNames() { |
| 117 | + const { data } = await IncidentCommander.get<{ name: string }[]>("/people", { |
| 118 | + params: { |
| 119 | + select: "name", |
| 120 | + type: "eq.service_account", |
| 121 | + deleted_at: "is.null", |
| 122 | + order: "name.asc" |
| 123 | + } |
| 124 | + }); |
| 125 | + return (data ?? []).map(({ name }) => name); |
| 126 | +} |
0 commit comments