-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: changed API of Session
- Loading branch information
Ivan Zuev
committed
Dec 24, 2021
1 parent
1e2dcc2
commit 826e6de
Showing
10 changed files
with
279 additions
and
43 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,2 +1,3 @@ | ||
export * as serviceClients from './generated/yandex/cloud/service_clients'; | ||
export * as cloudApi from './generated/yandex/cloud'; | ||
export * from './session'; |
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
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
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
ChannelCredentials, credentials, Metadata, ServiceDefinition, | ||
} from '@grpc/grpc-js'; | ||
import { createChannel } from 'nice-grpc'; | ||
import { | ||
GeneratedServiceClientCtor, | ||
IamTokenCredentialsConfig, | ||
OAuthCredentialsConfig, | ||
ServiceAccountCredentialsConfig, | ||
SessionConfig, | ||
} from './types'; | ||
import { IamTokenService } from './token-service/iam-token-service'; | ||
import { MetadataTokenService } from './token-service/metadata-token-service'; | ||
import { clientFactory } from './utils/client-factory'; | ||
import { serviceClients, cloudApi } from '.'; | ||
import { getServiceClientEndpoint } from './service-endpoints'; | ||
|
||
const isOAuth = (config: SessionConfig): config is OAuthCredentialsConfig => 'oauthToken' in config; | ||
|
||
const isIamToken = (config: SessionConfig): config is IamTokenCredentialsConfig => 'iamToken' in config; | ||
|
||
const isServiceAccount = (config: SessionConfig): config is ServiceAccountCredentialsConfig => 'serviceAccountJson' in config; | ||
|
||
const createIamToken = async (iamEndpoint: string, req: Partial<cloudApi.iam.iam_token.CreateIamTokenRequest>) => { | ||
const channel = createChannel(iamEndpoint, credentials.createSsl()); | ||
const client = clientFactory.create(serviceClients.IamTokenServiceClient.service, channel); | ||
const resp = await client.create(cloudApi.iam.iam_token.CreateIamTokenRequest.fromPartial(req)); | ||
|
||
return resp.iamToken; | ||
}; | ||
|
||
const newTokenCreator = (config: SessionConfig): () => Promise<string> => { | ||
if (isOAuth(config)) { | ||
return () => { | ||
const iamEndpoint = getServiceClientEndpoint(serviceClients.IamTokenServiceClient); | ||
|
||
return createIamToken(iamEndpoint, { | ||
yandexPassportOauthToken: config.oauthToken, | ||
}); | ||
}; | ||
} if (isIamToken(config)) { | ||
const { iamToken } = config; | ||
|
||
return async () => iamToken; | ||
} | ||
|
||
const tokenService = isServiceAccount(config) ? new IamTokenService(config.serviceAccountJson) : new MetadataTokenService(); | ||
|
||
return async () => tokenService.getToken(); | ||
}; | ||
|
||
const newChannelCredentials = (tokenCreator: TokenCreator) => credentials.combineChannelCredentials( | ||
credentials.createSsl(), | ||
credentials.createFromMetadataGenerator( | ||
( | ||
params: { service_url: string }, | ||
callback: (error: any, result?: any) => void, | ||
) => { | ||
tokenCreator() | ||
.then((token) => { | ||
const md = new Metadata(); | ||
|
||
md.set('authorization', `Bearer ${token}`); | ||
|
||
return callback(null, md); | ||
}) | ||
.catch((error) => callback(error)); | ||
}, | ||
), | ||
); | ||
|
||
type TokenCreator = () => Promise<string>; | ||
|
||
export class Session { | ||
private readonly config: SessionConfig; | ||
private readonly channelCredentials: ChannelCredentials; | ||
private readonly tokenCreator: TokenCreator; | ||
|
||
private static readonly DEFAULT_CONFIG: SessionConfig = { | ||
pollInterval: 1000, | ||
}; | ||
|
||
constructor(config?: SessionConfig) { | ||
this.config = { | ||
...Session.DEFAULT_CONFIG, | ||
...config, | ||
}; | ||
this.tokenCreator = newTokenCreator(this.config); | ||
this.channelCredentials = newChannelCredentials(this.tokenCreator); | ||
} | ||
|
||
get pollInterval() { | ||
return this.config.pollInterval; | ||
} | ||
|
||
client<S extends ServiceDefinition>(clientClass: GeneratedServiceClientCtor<S>) { | ||
const endpoint = getServiceClientEndpoint(clientClass); | ||
const channel = createChannel(endpoint, this.channelCredentials); | ||
|
||
return clientFactory.create(clientClass.service, channel); | ||
} | ||
} |
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.