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: support more succinct syntax for credential providers #1103

Merged
merged 3 commits into from
Jan 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import {
TopicClient,
PreviewVectorIndexClient,
PreviewLeaderboardClient,
CredentialProvider,
} from '../../src';
import {credsProvider} from './integration-setup';

// These tokens have valid syntax, but they don't actually have valid credentials. Just used for unit testing.
const fakeTestV1ApiKey =
'eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo=';

describe('default configurations', () => {
it('CacheClient should be able to be constructed with a default configuration', async () => {
const cacheClientViaConstructor = new CacheClient({
Expand All @@ -21,6 +26,31 @@ describe('default configurations', () => {
expect(cacheClientViaFactory).toBeInstanceOf(CacheClient);
});

it('CacheClient should be able to be constructed with a simple string for env var', () => {
const cacheClientViaConstructor = new CacheClient({
credentialProvider:
CredentialProvider.fromEnvironmentVariable('TEST_AUTH_TOKEN'),
defaultTtlSeconds: 60,
});
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('CacheClient should be able to be constructed with a simple string for env var, using short function name', () => {
const cacheClientViaConstructor = new CacheClient({
credentialProvider: CredentialProvider.fromEnvVar('TEST_AUTH_TOKEN'),
defaultTtlSeconds: 60,
});
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('CacheClient should be able to be constructed with a simple string for fromString', () => {
const cacheClientViaConstructor = new CacheClient({
credentialProvider: CredentialProvider.fromString(fakeTestV1ApiKey),
defaultTtlSeconds: 60,
});
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('TopicClient should be able to be constructed with a default configuration', () => {
const topicClientViaConstructor = new TopicClient({
credentialProvider: credsProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import {
TopicClient,
PreviewVectorIndexClient,
PreviewLeaderboardClient,
CredentialProvider,
} from '../../src';
import {credsProvider} from './integration-setup';

// These tokens have valid syntax, but they don't actually have valid credentials. Just used for unit testing.
const fakeTestV1ApiKey =
'eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo=';

describe('default configurations', () => {
it('CacheClient should be able to be constructed with a default configuration', () => {
const cacheClientViaConstructor = new CacheClient({
Expand All @@ -15,6 +20,31 @@ describe('default configurations', () => {
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('CacheClient should be able to be constructed with a simple string for env var', () => {
const cacheClientViaConstructor = new CacheClient({
credentialProvider:
CredentialProvider.fromEnvironmentVariable('TEST_AUTH_TOKEN'),
defaultTtlSeconds: 60,
});
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('CacheClient should be able to be constructed with a simple string for env var, using short function name', () => {
const cacheClientViaConstructor = new CacheClient({
credentialProvider: CredentialProvider.fromEnvVar('TEST_AUTH_TOKEN'),
defaultTtlSeconds: 60,
});
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('CacheClient should be able to be constructed with a simple string for fromString', () => {
const cacheClientViaConstructor = new CacheClient({
credentialProvider: CredentialProvider.fromString(fakeTestV1ApiKey),
defaultTtlSeconds: 60,
});
expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient);
});

it('TopicClient should be able to be constructed with a default configuration', () => {
const topicClientViaConstructor = new TopicClient({
credentialProvider: credsProvider(),
Expand Down
20 changes: 16 additions & 4 deletions packages/core/src/auth/credential-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,19 @@ export abstract class CredentialProvider {
abstract areEndpointsOverridden(): boolean;

static fromEnvironmentVariable(
props: EnvMomentoTokenProviderProps
props: EnvMomentoTokenProviderProps | string
): CredentialProvider {
return new EnvMomentoTokenProvider(props);
}

static fromEnvVar(
props: EnvMomentoTokenProviderProps | string
): CredentialProvider {
return new EnvMomentoTokenProvider(props);
}

static fromString(
props: StringMomentoTokenProviderProps
props: StringMomentoTokenProviderProps | string
): CredentialProvider {
return new StringMomentoTokenProvider(props);
}
Expand Down Expand Up @@ -140,7 +146,10 @@ export class StringMomentoTokenProvider extends CredentialProviderBase {
/**
* @param {StringMomentoTokenProviderProps} props configuration options for the token provider
*/
constructor(props: StringMomentoTokenProviderProps) {
constructor(props: StringMomentoTokenProviderProps | string) {
if (typeof props === 'string') {
props = {apiKey: props};
}
super();
let key: string;
if ('authToken' in props) {
Expand Down Expand Up @@ -238,7 +247,10 @@ export class EnvMomentoTokenProvider extends StringMomentoTokenProvider {
/**
* @param {EnvMomentoTokenProviderProps} props configuration options for the token provider
*/
constructor(props: EnvMomentoTokenProviderProps) {
constructor(props: EnvMomentoTokenProviderProps | string) {
if (typeof props === 'string') {
props = {environmentVariableName: props};
}
const authToken = process.env[props.environmentVariableName];
if (!authToken) {
throw new Error(
Expand Down
Loading