Skip to content

Commit

Permalink
Move the credentials cache to the .rush-user folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
iclanton committed Dec 25, 2020
1 parent 1593caf commit 9d6af55
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 37 deletions.
11 changes: 3 additions & 8 deletions apps/rush-lib/src/api/BuildCacheConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '../logic/buildCache/AzureStorageBuildCacheProvider';
import { RushConfiguration } from './RushConfiguration';
import { FileSystemBuildCacheProvider } from '../logic/buildCache/FileSystemBuildCacheProvider';
import { RushGlobalFolder } from './RushGlobalFolder';
import { RushConstants } from '../logic/RushConstants';
import { RushUserConfiguration } from './RushUserConfiguration';

Expand Down Expand Up @@ -69,7 +68,6 @@ interface IBuildCacheConfigurationOptions {
buildCacheJson: IBuildCacheJson;
rushConfiguration: RushConfiguration;
rushUserConfiguration: RushUserConfiguration;
rushGlobalFolder: RushGlobalFolder;
}

/**
Expand All @@ -87,7 +85,7 @@ export class BuildCacheConfiguration {
public readonly cacheProvider: BuildCacheProviderBase;

private constructor(options: IBuildCacheConfigurationOptions) {
const { buildCacheJson, rushConfiguration, rushUserConfiguration, rushGlobalFolder } = options;
const { buildCacheJson, rushConfiguration, rushUserConfiguration } = options;
this.projectOutputFolderNames = buildCacheJson.projectOutputFolderNames;

switch (buildCacheJson.cacheProvider) {
Expand All @@ -104,7 +102,6 @@ export class BuildCacheConfiguration {
const azureStorageConfigurationJson: IAzureStorageConfigurationJson =
azureStorageBuildCacheJson.azureBlobStorageConfiguration;
this.cacheProvider = new AzureStorageBuildCacheProvider({
rushGlobalFolder,
storageAccountName: azureStorageConfigurationJson.storageAccountName,
storageContainerName: azureStorageConfigurationJson.storageContainerName,
azureEnvironment: azureStorageConfigurationJson.azureEnvironment,
Expand All @@ -125,8 +122,7 @@ export class BuildCacheConfiguration {
* If the file has not been created yet, then undefined is returned.
*/
public static async loadFromDefaultPathAsync(
rushConfiguration: RushConfiguration,
rushGlobalFolder: RushGlobalFolder
rushConfiguration: RushConfiguration
): Promise<BuildCacheConfiguration | undefined> {
const jsonFilePath: string = BuildCacheConfiguration.getBuildCacheConfigFilePath(rushConfiguration);
if (FileSystem.exists(jsonFilePath)) {
Expand All @@ -138,8 +134,7 @@ export class BuildCacheConfiguration {
return new BuildCacheConfiguration({
buildCacheJson,
rushConfiguration,
rushUserConfiguration,
rushGlobalFolder
rushUserConfiguration
});
} else {
return undefined;
Expand Down
17 changes: 11 additions & 6 deletions apps/rush-lib/src/api/RushUserConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ export class RushUserConfiguration {
}

public static async initializeAsync(): Promise<RushUserConfiguration> {
const homeFolderPath: string = Utilities.getHomeFolder();
const rushUserSettingsFilePath: string = path.join(
homeFolderPath,
RushConstants.rushUserConfigurationFolderName,
'settings.json'
);
const rushUserFolderPath: string = RushUserConfiguration.getRushUserFolderPath();
const rushUserSettingsFilePath: string = path.join(rushUserFolderPath, 'settings.json');
let rushUserSettingsJson: IRushUserSettingsJson | undefined;
try {
rushUserSettingsJson = await JsonFile.loadAndValidateAsync(
Expand All @@ -54,4 +50,13 @@ export class RushUserConfiguration {

return new RushUserConfiguration(rushUserSettingsJson);
}

public static getRushUserFolderPath(): string {
const homeFolderPath: string = Utilities.getHomeFolder();
const rushUserSettingsFilePath: string = path.join(
homeFolderPath,
RushConstants.rushUserConfigurationFolderName
);
return rushUserSettingsFilePath;
}
}
5 changes: 1 addition & 4 deletions apps/rush-lib/src/cli/actions/UpdateCloudCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ export class UpdateCloudCredentials extends BaseRushAction {

const buildCacheConfiguration:
| BuildCacheConfiguration
| undefined = await BuildCacheConfiguration.loadFromDefaultPathAsync(
this.rushConfiguration,
this.rushGlobalFolder
);
| undefined = await BuildCacheConfiguration.loadFromDefaultPathAsync(this.rushConfiguration);

if (!buildCacheConfiguration) {
const buildCacheConfigurationFilePath: string = BuildCacheConfiguration.getBuildCacheConfigFilePath(
Expand Down
5 changes: 1 addition & 4 deletions apps/rush-lib/src/cli/scriptActions/BulkScriptAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ export class BulkScriptAction extends BaseScriptAction {

const buildCacheConfiguration:
| BuildCacheConfiguration
| undefined = await BuildCacheConfiguration.loadFromDefaultPathAsync(
this.rushConfiguration,
this.rushGlobalFolder
);
| undefined = await BuildCacheConfiguration.loadFromDefaultPathAsync(this.rushConfiguration);

const taskSelector: TaskSelector = new TaskSelector({
rushConfiguration: this.rushConfiguration,
Expand Down
9 changes: 4 additions & 5 deletions apps/rush-lib/src/logic/CredentialCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import * as path from 'path';
import { FileSystem, JsonFile, JsonSchema, LockFile } from '@rushstack/node-core-library';

import { RushGlobalFolder } from '../api/RushGlobalFolder';
import { IDisposable, Utilities } from '../utilities/Utilities';
import { RushUserConfiguration } from '../api/RushUserConfiguration';

const CACHE_FILENAME: string = 'credentials.json';
const LATEST_CREDENTIALS_JSON_VERSION: string = '0.1.0';
Expand All @@ -28,7 +28,6 @@ export interface ICredentialCacheEntry {
}

export interface ICredentialCacheOptions {
rushGlobalFolder: RushGlobalFolder;
supportEditing: boolean;
}

Expand Down Expand Up @@ -59,8 +58,8 @@ export class CredentialCache implements IDisposable {
}

public static async initializeAsync(options: ICredentialCacheOptions): Promise<CredentialCache> {
const rushGlobalFolderPath: string = options.rushGlobalFolder.path;
const cacheFilePath: string = path.join(rushGlobalFolderPath, CACHE_FILENAME);
const rushUserFolderPath: string = RushUserConfiguration.getRushUserFolderPath();
const cacheFilePath: string = path.join(rushUserFolderPath, CACHE_FILENAME);
const jsonSchema: JsonSchema = JsonSchema.fromFile(
path.resolve(__dirname, '..', 'schemas', 'credentials.schema.json')
);
Expand All @@ -76,7 +75,7 @@ export class CredentialCache implements IDisposable {

let lockfile: LockFile | undefined;
if (options.supportEditing) {
lockfile = await LockFile.acquire(rushGlobalFolderPath, `${CACHE_FILENAME}.lock`);
lockfile = await LockFile.acquire(rushUserFolderPath, `${CACHE_FILENAME}.lock`);
}

const credentialCache: CredentialCache = new CredentialCache(cacheFilePath, loadedJson, lockfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { AzureAuthorityHosts, DeviceCodeCredential, DeviceCodeInfo } from '@azure/identity';

import { EnvironmentConfiguration, EnvironmentVariableNames } from '../../api/EnvironmentConfiguration';
import { RushGlobalFolder } from '../../api/RushGlobalFolder';
import { CredentialCache, ICredentialCacheEntry } from '../CredentialCache';
import { URLSearchParams } from 'url';
import { RushConstants } from '../RushConstants';
Expand All @@ -31,7 +30,6 @@ export interface IAzureStorageBuildCacheProviderOptions extends IBuildCacheProvi
azureEnvironment?: AzureEnvironmentNames;
blobPrefix?: string;
isCacheWriteAllowed: boolean;
rushGlobalFolder: RushGlobalFolder;
}

const SAS_TTL_MILLISECONDS: number = 7 * 24 * 60 * 60 * 1000; // Seven days
Expand All @@ -42,7 +40,6 @@ export class AzureStorageBuildCacheProvider extends BuildCacheProviderBase {
private readonly _azureEnvironment: AzureEnvironmentNames;
private readonly _blobPrefix: string | undefined;
private readonly _isCacheWriteAllowed: boolean;
private readonly _rushGlobalFolder: RushGlobalFolder;
private __credentialCacheId: string | undefined;

private _containerClient: ContainerClient | undefined;
Expand All @@ -54,7 +51,6 @@ export class AzureStorageBuildCacheProvider extends BuildCacheProviderBase {
this._azureEnvironment = options.azureEnvironment || 'AzurePublicCloud';
this._blobPrefix = options.blobPrefix;
this._isCacheWriteAllowed = options.isCacheWriteAllowed;
this._rushGlobalFolder = options.rushGlobalFolder;

if (!(this._azureEnvironment in AzureAuthorityHosts)) {
throw new Error(
Expand Down Expand Up @@ -119,7 +115,6 @@ export class AzureStorageBuildCacheProvider extends BuildCacheProviderBase {
public async updateCachedCredentialAsync(terminal: Terminal, credential: string): Promise<void> {
await CredentialCache.usingAsync(
{
rushGlobalFolder: this._rushGlobalFolder,
supportEditing: true
},
async (credentialsCache: CredentialCache) => {
Expand All @@ -135,7 +130,6 @@ export class AzureStorageBuildCacheProvider extends BuildCacheProviderBase {

await CredentialCache.usingAsync(
{
rushGlobalFolder: this._rushGlobalFolder,
supportEditing: true
},
async (credentialsCache: CredentialCache) => {
Expand All @@ -148,7 +142,6 @@ export class AzureStorageBuildCacheProvider extends BuildCacheProviderBase {
public async deleteCachedCredentialsAsync(terminal: Terminal): Promise<void> {
await CredentialCache.usingAsync(
{
rushGlobalFolder: this._rushGlobalFolder,
supportEditing: true
},
async (credentialsCache: CredentialCache) => {
Expand All @@ -171,7 +164,6 @@ export class AzureStorageBuildCacheProvider extends BuildCacheProviderBase {
let cacheEntry: ICredentialCacheEntry | undefined;
await CredentialCache.usingAsync(
{
rushGlobalFolder: this._rushGlobalFolder,
supportEditing: false
},
(credentialsCache: CredentialCache) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ describe('AzureStorageBuildCacheProvider', () => {
storageAccountName: 'storage-account',
storageContainerName: 'container-name',
azureEnvironment: 'INCORRECT_AZURE_ENVIRONMENT' as AzureEnvironmentNames,
isCacheWriteAllowed: false,
rushGlobalFolder: undefined!
isCacheWriteAllowed: false
})
).toThrowErrorMatchingSnapshot();
});
Expand Down

0 comments on commit 9d6af55

Please sign in to comment.