Skip to content

Commit

Permalink
Fix/privatisation quickie interaction (#1094)
Browse files Browse the repository at this point in the history
* fix: modify both staging and staging-lite sites when modifying passwords

* fix: adjust behaviour of staging url retrieval to have private sites take priority

* chore: update privatisation status after amplify call succeeds
  • Loading branch information
alexanderleegs authored Jan 17, 2024
1 parent d25129c commit 42118c5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
14 changes: 9 additions & 5 deletions src/services/configServices/SettingsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ class SettingsService {
// only when changing from public to private - not awaited as this is slow and non-blocking
privatiseNetlifySite(siteName, password)
}
const updatePasswordResp = await this.deploymentsService.updateAmplifyPassword(
siteName,
password,
enablePassword
)
if (updatePasswordResp.isErr()) {
return updatePasswordResp
}
if (isPrivate !== enablePassword) {
// For public -> private or private -> public, we also need to update the repo privacy on github
const privatiseRepoRes = await this.gitHubService.changeRepoPrivacy(
Expand All @@ -189,11 +197,7 @@ class SettingsService {
return errAsync(err)
}
}
return this.deploymentsService.updateAmplifyPassword(
siteName,
password,
enablePassword
)
return updatePasswordResp
}

shouldUpdateHomepage(updatedConfigContent, configContent) {
Expand Down
12 changes: 8 additions & 4 deletions src/services/identity/DeploymentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,23 @@ class DeploymentClient {
},
})

generateDeletePasswordInput = (appId: string): UpdateBranchCommandInput => ({
generateDeletePasswordInput = (
appId: string,
branchName = "staging"
): UpdateBranchCommandInput => ({
appId,
branchName: "staging",
branchName,
enableBasicAuth: false,
basicAuthCredentials: "",
})

generateUpdatePasswordInput = (
appId: string,
password: string
password: string,
branchName = "staging"
): UpdateBranchCommandInput => ({
appId,
branchName: "staging",
branchName,
enableBasicAuth: true,
basicAuthCredentials: Buffer.from(`user:${password}`).toString("base64"),
})
Expand Down
44 changes: 40 additions & 4 deletions src/services/identity/DeploymentsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ class DeploymentsService {
return okAsync(deploymentInfo)
}

deletePassword = async (appId: string, deploymentId: number) => {
deletePassword = async (
appId: string,
deploymentId: number,
isStagingLite: boolean
) => {
const updateAppInput = this.deploymentClient.generateDeletePasswordInput(
appId
appId,
isStagingLite ? "staging-lite" : "staging"
)
const updateResp = await this.deploymentClient.sendUpdateApp(updateAppInput)

Expand Down Expand Up @@ -249,9 +254,22 @@ class DeploymentsService {
return errAsync(
new NotFoundError(`Deployment for site ${repoName} does not exist`)
)
const { id, hostingId: appId } = deploymentInfo
const {
id,
hostingId: appId,
stagingLiteHostingId: stagingLiteId,
} = deploymentInfo

if (!enablePassword) return this.deletePassword(appId, id)
if (!enablePassword) {
const stagingRes = await this.deletePassword(appId, id, false)
if (!stagingLiteId || stagingRes.isErr()) return stagingRes
const stagingLiteRes = await this.deletePassword(stagingLiteId, id, true)
if (stagingLiteRes.isErr())
logger.error(
`Privatisation adjustment failed for ${repoName} - requires manual fixing of inconsistent state`
)
return stagingLiteRes
}

const {
encryptedPassword: oldEncryptedPassword,
Expand All @@ -273,6 +291,24 @@ class DeploymentsService {
return updateResp
}

if (stagingLiteId) {
const updateStagingLiteInput = this.deploymentClient.generateUpdatePasswordInput(
stagingLiteId,
password,
"staging-lite"
)

const updateStagingLiteResp = await this.deploymentClient.sendUpdateApp(
updateStagingLiteInput
)
if (updateStagingLiteResp.isErr()) {
logger.error(
`Privatisation adjustment failed for ${repoName} - requires manual fixing of inconsistent state`
)
return updateStagingLiteResp
}
}

const { encryptedPassword, iv } = encryptPassword(password, SECRET_KEY)
await this.deploymentsRepository.update(
{
Expand Down
23 changes: 17 additions & 6 deletions src/services/identity/SitesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,30 @@ class SitesService {
return okAsync(site.deployment)
}

// Privatisation has priority over growthbook - if private, automatically use staging
const isPrivateSiteSyncedWithDb =
site.isPrivate && site?.deployment?.stagingUrl.includes("staging.")

const featureFlagSyncedWithDb =
(isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) &&
!site.isPrivate &&
((isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) &&
site?.deployment?.stagingUrl.includes("staging-lite.")) ||
// useful for rollbacks
(!isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) &&
site?.deployment?.stagingUrl.includes("staging."))
// useful for rollbacks
(!isReduceBuildTimesWhitelistedRepo(sessionData.growthbook) &&
site?.deployment?.stagingUrl.includes("staging.")))

if (featureFlagSyncedWithDb) {
if (isPrivateSiteSyncedWithDb || featureFlagSyncedWithDb) {
return okAsync(site.deployment)
}

let stagingUrl: StagingPermalink
if (isReduceBuildTimesWhitelistedRepo(sessionData.growthbook)) {
if (site.isPrivate) {
stagingUrl = Brand.fromString(
`https://staging.${site.deployment.hostingId}.amplifyapp.com`
)
} else if (
isReduceBuildTimesWhitelistedRepo(sessionData.growthbook)
) {
stagingUrl = Brand.fromString(
`https://staging-lite.${site.deployment.stagingLiteHostingId}.amplifyapp.com`
)
Expand Down

0 comments on commit 42118c5

Please sign in to comment.