Skip to content
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

feat: move getCachePlugin to shared #799

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/helix-shared-tokencache/src/FSCachePlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ export declare class FSCachePlugin implements ICachePlugin {
afterCacheAccess(tokenCacheContext: TokenCacheContext): Promise<boolean>;

beforeCacheAccess(tokenCacheContext: TokenCacheContext): Promise<boolean>;

location: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all cache plugins now have a location

}
4 changes: 4 additions & 0 deletions packages/helix-shared-tokencache/src/FSCachePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ export class FSCachePlugin {
}
return false;
}

get location() {
return this.filePath;
}
}
2 changes: 2 additions & 0 deletions packages/helix-shared-tokencache/src/MemCachePlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ export declare class MemCachePlugin implements ICachePlugin {
afterCacheAccess(tokenCacheContext: TokenCacheContext): Promise<boolean>;

beforeCacheAccess(tokenCacheContext: TokenCacheContext): Promise<boolean>;

location: string;
}
4 changes: 4 additions & 0 deletions packages/helix-shared-tokencache/src/MemCachePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ export class MemCachePlugin {
}
return false;
}

get location() {
return this.base ? this.base.location : this.key;
}
Comment on lines +89 to +91
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when a MemCachePlugin has a base plugin, show that location

}
2 changes: 2 additions & 0 deletions packages/helix-shared-tokencache/src/S3CachePlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export declare class S3CachePlugin implements ICachePlugin {

deleteCache(): Promise<void>;

location: string;

afterCacheAccess(tokenCacheContext: TokenCacheContext): Promise<boolean>;

beforeCacheAccess(tokenCacheContext: TokenCacheContext): Promise<boolean>;
Expand Down
4 changes: 4 additions & 0 deletions packages/helix-shared-tokencache/src/S3CachePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export class S3CachePlugin {
}
return false;
}

get location() {
return `${this.bucket}/${this.key}`;
}
Comment on lines +114 to +116
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for S3, the location is just the S3 URI

}

S3CachePlugin.encrypt = encrypt;
Expand Down
52 changes: 52 additions & 0 deletions packages/helix-shared-tokencache/src/getCachePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-disable no-param-reassign */

import { MemCachePlugin } from './MemCachePlugin.js';
import { S3CacheManager } from './S3CacheManager.js';

/**
* Returns the S3 cache plugin
*
* @returns {ICachePlugin} the cache plugin
*/
export async function getCachePlugin(opts, type) {
const { log, contentBusId } = opts;
Copy link
Contributor Author

@dominique-pfister dominique-pfister May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the content bus id is expected in opts, all clients currently have an extra context as first argument in their implementation of getCachePlugin.


const derivedOpts = [];
if (contentBusId) {
derivedOpts.push({
prefix: `${contentBusId}/.helix-auth`,
secret: contentBusId,
});
}
const basePlugin = await S3CacheManager.findCache('content', {
log,
prefix: 'default/.helix-auth',
secret: 'default',
bucket: 'helix-content-bus',
type,
}, ...derivedOpts);

log.info(`using connected user from ${basePlugin.location}`);

/** @type MemCachePluginOptions */
const cacheOpts = {
log,
key: basePlugin.key,
base: basePlugin,
};
if (process.env.HELIX_ONEDRIVE_LOCAL_AUTH_CACHE) {
cacheOpts.caches = new Map();
}
return new MemCachePlugin(cacheOpts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('FSCachePlugin Test', () => {
assert.deepStrictEqual(JSON.parse(await fs.readFile(testFilePath, 'utf-8')), {
access_token: '1234',
});
assert.strictEqual(p.location, testFilePath);
});

it('does not the cache data to the filesystem if context not changed', async () => {
Expand Down
66 changes: 66 additions & 0 deletions packages/helix-shared-tokencache/test/getCachePlugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
import assert from 'assert';
import { getCachePlugin } from '../src/getCachePlugin.js';
import { Nock } from './utils.js';

const DEFAULT_ENV = {
AZURE_HELIX_SERVICE_CLIENT_ID: 'client-id',
AZURE_HELIX_SERVICE_CLIENT_SECRET: 'client-secret',
};

describe('getCachePlugin tests', () => {
let nock;
let savedProcessEnv;
beforeEach(() => {
nock = new Nock();
savedProcessEnv = process.env;
process.env = {
...process.env,
AWS_REGION: 'us-east-1',
AWS_ACCESS_KEY_ID: 'fake-key-id',
AWS_SECRET_ACCESS_KEY: 'fake-secret',
};
});

afterEach(() => {
nock.done();
process.env = savedProcessEnv;
});

it('uses derived opts if contentBusId is available', async () => {
const contentBusId = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789a';
nock('https://helix-content-bus.s3.us-east-1.amazonaws.com')
.head(`/${contentBusId}/.helix-auth/auth-onedrive-content.json`)
.reply(200);

const cachePlugin = await getCachePlugin({
log: console,
env: DEFAULT_ENV,
contentBusId,
}, 'onedrive');
assert.ok(cachePlugin);
assert.strictEqual(cachePlugin.location, `helix-content-bus/${contentBusId}/.helix-auth/auth-onedrive-content.json`);
});

it('uses empty opts if contentBusId is not available', async () => {
// "content-bus-id/.helix-auth/auth-onedrive-content.json"
const cachePlugin = await getCachePlugin({
log: console,
env: DEFAULT_ENV,
contentBusId: null,
}, 'onedrive');
assert.ok(cachePlugin);
assert.strictEqual(cachePlugin.location, 'helix-content-bus/default/.helix-auth/auth-onedrive-content.json');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('MemCachePlugin Test', () => {
const ret = await p.afterCacheAccess(ctx);
assert.strictEqual(ret, true);
assert.strictEqual(caches.get('foobar-key'), 'foobar');
assert.strictEqual(p.location, 'foobar-key');
});

it('read/writes the cache w/o base with global cache', async () => {
Expand Down