-
Notifications
You must be signed in to change notification settings - Fork 938
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
Remove distinction between build and run accounts #6952
Open
inlined
wants to merge
2
commits into
master
Choose a base branch
from
inlined.secrets-set-cleanup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import * as clc from "colorette"; | ||
const Table = require("cli-table"); | ||
|
||
import { MultiServiceAccounts, ServiceAccounts, serviceAccountsForBackend, toMulti } from "."; | ||
import { serviceAccountsForBackend } from "."; | ||
import * as apphosting from "../../gcp/apphosting"; | ||
import * as prompt from "../../prompt"; | ||
import * as utils from "../../utils"; | ||
|
@@ -13,8 +13,7 @@ import * as env from "../../functions/env"; | |
interface BackendMetadata { | ||
location: string; | ||
id: string; | ||
buildServiceAccount: string; | ||
runServiceAccount: string; | ||
serviceAccounts: string[]; | ||
} | ||
|
||
/** | ||
|
@@ -28,7 +27,11 @@ export function toMetadata( | |
for (const backend of backends) { | ||
// Splits format projects/<unused>/locations/<location>/backends/<id> | ||
const [, , , location, , id] = backend.name.split("/"); | ||
metadata.push({ location, id, ...serviceAccountsForBackend(projectNumber, backend) }); | ||
metadata.push({ | ||
location, | ||
id, | ||
serviceAccounts: serviceAccountsForBackend(projectNumber, backend), | ||
}); | ||
} | ||
return metadata.sort((left, right) => { | ||
const cmplocation = left.location.localeCompare(right.location); | ||
|
@@ -39,22 +42,10 @@ export function toMetadata( | |
}); | ||
} | ||
|
||
/** Displays a single service account or a comma separated list of service accounts. */ | ||
export function serviceAccountDisplay(metadata: ServiceAccounts): string { | ||
if (sameServiceAccount(metadata)) { | ||
return metadata.runServiceAccount; | ||
} | ||
return `${metadata.buildServiceAccount}, ${metadata.runServiceAccount}`; | ||
} | ||
|
||
function sameServiceAccount(metadata: ServiceAccounts): boolean { | ||
return metadata.buildServiceAccount === metadata.runServiceAccount; | ||
} | ||
|
||
const matchesServiceAccounts = (target: ServiceAccounts) => (test: ServiceAccounts) => { | ||
const matchesServiceAccounts = (target: BackendMetadata) => (test: BackendMetadata) => { | ||
return ( | ||
target.buildServiceAccount === test.buildServiceAccount && | ||
target.runServiceAccount === test.runServiceAccount | ||
target.serviceAccounts.length === test.serviceAccounts.length && | ||
target.serviceAccounts.every((sa) => test.serviceAccounts.indexOf(sa) !== -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having BackendMetadata.serviceAccounts be a |
||
); | ||
}; | ||
|
||
|
@@ -68,38 +59,12 @@ export function tableForBackends( | |
const headers = [ | ||
"location", | ||
"backend", | ||
metadata.every(sameServiceAccount) ? "service account" : "service accounts", | ||
metadata.every((m) => m.serviceAccounts.length === 1) ? "service account" : "service accounts", | ||
]; | ||
const rows = metadata.map((m) => [m.location, m.id, serviceAccountDisplay(m)]); | ||
const rows = metadata.map((m) => [m.location, m.id, m.serviceAccounts.join(", ")]); | ||
return [headers, rows]; | ||
} | ||
|
||
/** | ||
* Returns a MultiServiceAccounts for all selected service accounts in a ServiceAccount[]. | ||
* If a service account is ever a "build" account in input, it will be a "build" account in the | ||
* output. Otherwise, it will be a "run" account. | ||
*/ | ||
export function selectFromMetadata( | ||
input: ServiceAccounts[], | ||
selected: string[], | ||
): MultiServiceAccounts { | ||
const buildAccounts = new Set<string>(); | ||
const runAccounts = new Set<string>(); | ||
|
||
for (const sa of selected) { | ||
if (input.find((m) => m.buildServiceAccount === sa)) { | ||
buildAccounts.add(sa); | ||
} else { | ||
runAccounts.add(sa); | ||
} | ||
} | ||
|
||
return { | ||
buildServiceAccounts: [...buildAccounts], | ||
runServiceAccounts: [...runAccounts], | ||
}; | ||
} | ||
|
||
/** Common warning log that there are no backends. Exported to make tests easier. */ | ||
export const WARN_NO_BACKENDS = | ||
"To use this secret, your backend's service account must have secret accessor permission. " + | ||
|
@@ -117,7 +82,7 @@ export async function selectBackendServiceAccounts( | |
projectNumber: string, | ||
projectId: string, | ||
options: any, | ||
): Promise<MultiServiceAccounts> { | ||
): Promise<string[]> { | ||
const listBackends = await apphosting.listBackends(projectId, "-"); | ||
|
||
if (listBackends.unreachable.length) { | ||
|
@@ -130,7 +95,7 @@ export async function selectBackendServiceAccounts( | |
|
||
if (!listBackends.backends.length) { | ||
utils.logLabeledWarning("apphosting", WARN_NO_BACKENDS); | ||
return { buildServiceAccounts: [], runServiceAccounts: [] }; | ||
return []; | ||
} | ||
|
||
if (listBackends.backends.length === 1) { | ||
|
@@ -141,10 +106,10 @@ export async function selectBackendServiceAccounts( | |
"To use this secret, your backend's service account must have secret accessor permission. Would you like to grant it now?", | ||
}); | ||
if (grant) { | ||
return toMulti(serviceAccountsForBackend(projectNumber, listBackends.backends[0])); | ||
return serviceAccountsForBackend(projectNumber, listBackends.backends[0]); | ||
} | ||
utils.logLabeledBullet("apphosting", GRANT_ACCESS_IN_FUTURE); | ||
return { buildServiceAccounts: [], runServiceAccounts: [] }; | ||
return []; | ||
} | ||
|
||
const metadata: BackendMetadata[] = toMetadata(projectNumber, listBackends.backends); | ||
|
@@ -153,8 +118,8 @@ export async function selectBackendServiceAccounts( | |
utils.logLabeledBullet( | ||
"apphosting", | ||
"To use this secret, your backend's service account must have secret accessor permission. All of your backends use " + | ||
(sameServiceAccount(metadata[0]) ? "service account " : "service accounts ") + | ||
serviceAccountDisplay(metadata[0]) + | ||
(metadata[0].serviceAccounts.length === 1 ? "service account " : "service accounts ") + | ||
metadata[0].serviceAccounts.join(", ") + | ||
". Granting access to one backend will grant access to all backends.", | ||
); | ||
const grant = await prompt.confirm({ | ||
|
@@ -163,13 +128,10 @@ export async function selectBackendServiceAccounts( | |
message: "Would you like to grant it now?", | ||
}); | ||
if (grant) { | ||
return selectFromMetadata(metadata, [ | ||
metadata[0].buildServiceAccount, | ||
metadata[0].runServiceAccount, | ||
]); | ||
return metadata[0].serviceAccounts; | ||
} | ||
utils.logLabeledBullet("apphosting", GRANT_ACCESS_IN_FUTURE); | ||
return { buildServiceAccounts: [], runServiceAccounts: [] }; | ||
return []; | ||
} | ||
|
||
utils.logLabeledBullet( | ||
|
@@ -185,8 +147,9 @@ export async function selectBackendServiceAccounts( | |
logger.info(table.toString()); | ||
|
||
const allAccounts = metadata.reduce((accum: Set<string>, row) => { | ||
accum.add(row.buildServiceAccount); | ||
accum.add(row.runServiceAccount); | ||
for (const sa of row.serviceAccounts) { | ||
accum.add(sa); | ||
} | ||
return accum; | ||
}, new Set<string>()); | ||
const chosen = await prompt.promptOnce({ | ||
|
@@ -199,7 +162,7 @@ export async function selectBackendServiceAccounts( | |
if (!chosen.length) { | ||
utils.logLabeledBullet("apphosting", GRANT_ACCESS_IN_FUTURE); | ||
} | ||
return selectFromMetadata(metadata, chosen); | ||
return chosen; | ||
} | ||
|
||
function toUpperSnakeCase(key: string): string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is updating this field to be a
Set
an option?