@@ -2,13 +2,13 @@ import { createOctoClient, createGithubAuth } from './gh-auth';
22import nock from 'nock' ;
33import { createAppAuth } from '@octokit/auth-app' ;
44import { StrategyOptions } from '@octokit/auth-app/dist-types/types' ;
5- import { decrypt } from './kms ' ;
5+ import SSM from './ssm ' ;
66import { RequestInterface } from '@octokit/types' ;
77import { mock , MockProxy } from 'jest-mock-extended' ;
88import { request } from '@octokit/request' ;
99
10- jest . mock ( './kms' ) ;
1110jest . mock ( '@octokit/auth-app' ) ;
11+ jest . mock ( './ssm' ) ;
1212
1313const cleanEnv = process . env ;
1414
@@ -19,7 +19,7 @@ beforeEach(() => {
1919 nock . disableNetConnect ( ) ;
2020} ) ;
2121
22- describe ( 'Test createGithubAuth ' , ( ) => {
22+ describe ( 'Test createOctoClient ' , ( ) => {
2323 test ( 'Creates app client to GitHub public' , async ( ) => {
2424 // Arrange
2525 const token = '123456' ;
@@ -46,56 +46,67 @@ describe('Test createGithubAuth', () => {
4646} ) ;
4747
4848describe ( 'Test createGithubAuth' , ( ) => {
49- const mockedDecrypt = ( decrypt as unknown ) as jest . Mock ;
49+ const mockedSSM = SSM as jest . MockedClass < typeof SSM > ;
5050 const mockedCreatAppAuth = ( createAppAuth as unknown ) as jest . Mock ;
5151 const mockedDefaults = jest . spyOn ( request , 'defaults' ) ;
5252 let mockedRequestInterface : MockProxy < RequestInterface > ;
5353
5454 const installationId = 1 ;
5555 const authType = 'app' ;
5656 const token = '123456' ;
57- const decryptedValue = 'decryptedValue' ;
58- const b64 = Buffer . from ( decryptedValue , 'binary' ) . toString ( 'base64' ) ;
57+ const privateKey = 'my-private-key' ;
58+ const privateKeyBase64 = Buffer . from ( privateKey , 'binary' ) . toString ( 'base64' ) ;
59+ const appId = '123' ;
60+ const clientId = 'abc' ;
61+ const clientSecret = 'abcdef123456' ;
5962
6063 beforeEach ( ( ) => {
61- process . env . GITHUB_APP_ID = '1 ' ;
62- process . env . GITHUB_APP_CLIENT_SECRET = 'client_secret ' ;
63- process . env . GITHUB_APP_KEY_BASE64 = 'base64 ' ;
64- process . env . KMS_KEY_ID = 'key_id ' ;
64+ process . env . GITHUB_APP_KEY_BASE64_PARAMETER_NAME = 'private-key ' ;
65+ process . env . GITHUB_APP_ID_PARAMETER_NAME = 'app-id ' ;
66+ process . env . GITHUB_APP_CLIENT_ID_PARAMETER_NAME = 'client-id ' ;
67+ process . env . GITHUB_APP_CLIENT_SECRET_PARAMETER_NAME = 'client-secret ' ;
6568 process . env . ENVIRONMENT = 'dev' ;
66- process . env . GITHUB_APP_CLIENT_ID = '1' ;
6769 } ) ;
6870
6971 test ( 'Creates auth object for public GitHub' , async ( ) => {
7072 // Arrange
7173 const authOptions = {
72- appId : parseInt ( process . env . GITHUB_APP_ID as string ) ,
73- privateKey : 'decryptedValue' ,
74+ appId : parseInt ( appId ) ,
75+ privateKey,
7476 installationId,
75- clientId : process . env . GITHUB_APP_CLIENT_ID ,
76- clientSecret : 'decryptedValue' ,
77+ clientId,
78+ clientSecret,
7779 } ;
7880
79- mockedDecrypt . mockResolvedValueOnce ( decryptedValue ) . mockResolvedValueOnce ( b64 ) ;
81+ const mockedGetParameter = jest . fn ( )
82+ . mockResolvedValueOnce ( privateKeyBase64 )
83+ . mockResolvedValueOnce ( appId )
84+ . mockResolvedValueOnce ( clientId )
85+ . mockResolvedValueOnce ( clientSecret ) ;
86+ mockedSSM . mockImplementation ( ( ) => ( {
87+ ssm : null as any ,
88+ getParameter : mockedGetParameter ,
89+ } ) ) ;
90+
8091 const mockedAuth = jest . fn ( ) ;
8192 mockedAuth . mockResolvedValue ( { token } ) ;
82- mockedCreatAppAuth . mockImplementation ( ( authOptions : StrategyOptions ) => {
83- return mockedAuth ;
84- } ) ;
93+ mockedCreatAppAuth . mockImplementation ( ( ) => mockedAuth ) ;
8594
8695 // Act
8796 const result = await createGithubAuth ( installationId , authType ) ;
8897
8998 // Assert
90- expect ( mockedDecrypt ) . toBeCalledWith (
91- process . env . GITHUB_APP_CLIENT_SECRET ,
92- process . env . KMS_KEY_ID ,
93- process . env . ENVIRONMENT ,
99+ expect ( mockedGetParameter ) . toBeCalledWith (
100+ process . env . GITHUB_APP_KEY_BASE64_PARAMETER_NAME
101+ ) ;
102+ expect ( mockedGetParameter ) . toBeCalledWith (
103+ process . env . GITHUB_APP_ID_PARAMETER_NAME
104+ ) ;
105+ expect ( mockedGetParameter ) . toBeCalledWith (
106+ process . env . GITHUB_APP_CLIENT_ID_PARAMETER_NAME
94107 ) ;
95- expect ( mockedDecrypt ) . toBeCalledWith (
96- process . env . GITHUB_APP_KEY_BASE64 ,
97- process . env . KMS_KEY_ID ,
98- process . env . ENVIRONMENT ,
108+ expect ( mockedGetParameter ) . toBeCalledWith (
109+ process . env . GITHUB_APP_CLIENT_SECRET_PARAMETER_NAME
99110 ) ;
100111 expect ( mockedCreatAppAuth ) . toBeCalledTimes ( 1 ) ;
101112 expect ( mockedCreatAppAuth ) . toBeCalledWith ( authOptions ) ;
@@ -113,34 +124,43 @@ describe('Test createGithubAuth', () => {
113124 } ) ;
114125
115126 const authOptions = {
116- appId : parseInt ( process . env . GITHUB_APP_ID as string ) ,
117- privateKey : 'decryptedValue' ,
127+ appId : parseInt ( appId ) ,
128+ privateKey,
118129 installationId,
119- clientId : process . env . GITHUB_APP_CLIENT_ID ,
120- clientSecret : 'decryptedValue' ,
130+ clientId,
131+ clientSecret,
121132 request : mockedRequestInterface . defaults ( { baseUrl : githubServerUrl } ) ,
122133 } ;
123134
124- mockedDecrypt . mockResolvedValueOnce ( decryptedValue ) . mockResolvedValueOnce ( b64 ) ;
135+ const mockedGetParameter = jest . fn ( )
136+ . mockResolvedValueOnce ( privateKeyBase64 )
137+ . mockResolvedValueOnce ( appId )
138+ . mockResolvedValueOnce ( clientId )
139+ . mockResolvedValueOnce ( clientSecret ) ;
140+ mockedSSM . mockImplementation ( ( ) => ( {
141+ ssm : null as any ,
142+ getParameter : mockedGetParameter ,
143+ } ) ) ;
144+
125145 const mockedAuth = jest . fn ( ) ;
126146 mockedAuth . mockResolvedValue ( { token } ) ;
127- mockedCreatAppAuth . mockImplementation ( ( authOptions : StrategyOptions ) => {
128- return mockedAuth ;
129- } ) ;
147+ mockedCreatAppAuth . mockImplementation ( ( ) => mockedAuth ) ;
130148
131149 // Act
132150 const result = await createGithubAuth ( installationId , authType , githubServerUrl ) ;
133151
134152 // Assert
135- expect ( mockedDecrypt ) . toBeCalledWith (
136- process . env . GITHUB_APP_CLIENT_SECRET ,
137- process . env . KMS_KEY_ID ,
138- process . env . ENVIRONMENT ,
153+ expect ( mockedGetParameter ) . toBeCalledWith (
154+ process . env . GITHUB_APP_KEY_BASE64_PARAMETER_NAME
155+ ) ;
156+ expect ( mockedGetParameter ) . toBeCalledWith (
157+ process . env . GITHUB_APP_ID_PARAMETER_NAME
158+ ) ;
159+ expect ( mockedGetParameter ) . toBeCalledWith (
160+ process . env . GITHUB_APP_CLIENT_ID_PARAMETER_NAME
139161 ) ;
140- expect ( mockedDecrypt ) . toBeCalledWith (
141- process . env . GITHUB_APP_KEY_BASE64 ,
142- process . env . KMS_KEY_ID ,
143- process . env . ENVIRONMENT ,
162+ expect ( mockedGetParameter ) . toBeCalledWith (
163+ process . env . GITHUB_APP_CLIENT_SECRET_PARAMETER_NAME
144164 ) ;
145165 expect ( mockedCreatAppAuth ) . toBeCalledTimes ( 1 ) ;
146166 expect ( mockedCreatAppAuth ) . toBeCalledWith ( authOptions ) ;
0 commit comments