1- import { getParameter } from '@aws-github-runner/aws-ssm-util' ;
2- import { getRunnerMatcherConfigStore , type RunnerMatcherConfigStore } from '@aws-github-runner/storage-providers' ;
1+ import {
2+ getGitHubWebhookSecretStore ,
3+ getRunnerMatcherConfigStore ,
4+ type GitHubWebhookSecretStore ,
5+ type RunnerMatcherConfigStore ,
6+ } from '@aws-github-runner/storage-providers' ;
37import { ConfigWebhook , ConfigWebhookEventBridge , ConfigDispatcher } from './ConfigLoader' ;
48
59import { logger } from '@aws-github-runner/aws-powertools-util' ;
610import { RunnerMatcherConfig } from './sqs' ;
711import { describe , it , expect , beforeEach , vi } from 'vitest' ;
812
9- vi . mock ( '@aws-github-runner/aws-ssm-util' ) ;
1013vi . mock ( '@aws-github-runner/storage-providers' ) ;
1114
15+ const githubWebhookSecretStore = {
16+ get : vi . fn ( ) ,
17+ } satisfies GitHubWebhookSecretStore ;
1218const runnerMatcherConfigStore = {
1319 get : vi . fn ( ) ,
1420} satisfies RunnerMatcherConfigStore ;
@@ -20,6 +26,7 @@ describe('ConfigLoader Tests', () => {
2026 ConfigWebhookEventBridge . reset ( ) ;
2127 ConfigDispatcher . reset ( ) ;
2228 logger . setLogLevel ( 'DEBUG' ) ;
29+ vi . mocked ( getGitHubWebhookSecretStore ) . mockReturnValue ( githubWebhookSecretStore ) ;
2330 vi . mocked ( getRunnerMatcherConfigStore ) . mockReturnValue ( runnerMatcherConfigStore ) ;
2431
2532 // clear process.env
@@ -31,7 +38,6 @@ describe('ConfigLoader Tests', () => {
3138 describe ( 'Check base object' , ( ) => {
3239 function setupConfiguration ( ) : void {
3340 process . env . EVENT_BUS_NAME = 'event-bus' ;
34- process . env . PARAMETER_GITHUB_APP_WEBHOOK_SECRET = '/path/to/webhook/secret' ;
3541 const matcherConfig = [
3642 {
3743 id : '1' ,
@@ -43,7 +49,7 @@ describe('ConfigLoader Tests', () => {
4349 } ,
4450 ] ;
4551 runnerMatcherConfigStore . get . mockResolvedValue ( JSON . stringify ( matcherConfig ) ) ;
46- vi . mocked ( getParameter ) . mockResolvedValue ( 'secret' ) ;
52+ githubWebhookSecretStore . get . mockResolvedValue ( 'secret' ) ;
4753 }
4854
4955 it ( 'should return the same instance of ConfigWebhook (singleton)' , async ( ) => {
@@ -52,7 +58,7 @@ describe('ConfigLoader Tests', () => {
5258 const config2 = await ConfigWebhook . load ( ) ;
5359
5460 expect ( config1 ) . toBe ( config2 ) ;
55- expect ( getParameter ) . toHaveBeenCalledOnce ( ) ;
61+ expect ( githubWebhookSecretStore . get ) . toHaveBeenCalledOnce ( ) ;
5662 expect ( runnerMatcherConfigStore . get ) . toHaveBeenCalledOnce ( ) ;
5763 } ) ;
5864
@@ -62,7 +68,7 @@ describe('ConfigLoader Tests', () => {
6268 const config2 = await ConfigWebhookEventBridge . load ( ) ;
6369
6470 expect ( config1 ) . toBe ( config2 ) ;
65- expect ( getParameter ) . toHaveBeenCalledTimes ( 1 ) ;
71+ expect ( githubWebhookSecretStore . get ) . toHaveBeenCalledOnce ( ) ;
6672 expect ( runnerMatcherConfigStore . get ) . not . toHaveBeenCalled ( ) ;
6773 } ) ;
6874
@@ -72,7 +78,7 @@ describe('ConfigLoader Tests', () => {
7278 const config2 = await ConfigDispatcher . load ( ) ;
7379
7480 expect ( config1 ) . toBe ( config2 ) ;
75- expect ( getParameter ) . not . toHaveBeenCalled ( ) ;
81+ expect ( githubWebhookSecretStore . get ) . not . toHaveBeenCalled ( ) ;
7682 expect ( runnerMatcherConfigStore . get ) . toHaveBeenCalledOnce ( ) ;
7783 } ) ;
7884
@@ -96,7 +102,6 @@ describe('ConfigLoader Tests', () => {
96102 describe ( 'ConfigWebhook' , ( ) => {
97103 it ( 'should load config successfully' , async ( ) => {
98104 process . env . REPOSITORY_ALLOW_LIST = '["repo1", "repo2"]' ;
99- process . env . PARAMETER_GITHUB_APP_WEBHOOK_SECRET = '/path/to/webhook/secret' ;
100105 const matcherConfig = [
101106 {
102107 id : '1' ,
@@ -108,7 +113,7 @@ describe('ConfigLoader Tests', () => {
108113 } ,
109114 ] ;
110115 runnerMatcherConfigStore . get . mockResolvedValue ( JSON . stringify ( matcherConfig ) ) ;
111- vi . mocked ( getParameter ) . mockResolvedValue ( 'secret' ) ;
116+ githubWebhookSecretStore . get . mockResolvedValue ( 'secret' ) ;
112117
113118 const config : ConfigWebhook = await ConfigWebhook . load ( ) ;
114119
@@ -118,7 +123,6 @@ describe('ConfigLoader Tests', () => {
118123 } ) ;
119124
120125 it ( 'should load config successfully' , async ( ) => {
121- process . env . PARAMETER_GITHUB_APP_WEBHOOK_SECRET = '/path/to/webhook/secret' ;
122126 const matcherConfig = [
123127 {
124128 id : '1' ,
@@ -130,7 +134,7 @@ describe('ConfigLoader Tests', () => {
130134 } ,
131135 ] ;
132136 runnerMatcherConfigStore . get . mockResolvedValue ( JSON . stringify ( matcherConfig ) ) ;
133- vi . mocked ( getParameter ) . mockResolvedValue ( 'secret' ) ;
137+ githubWebhookSecretStore . get . mockResolvedValue ( 'secret' ) ;
134138
135139 const config : ConfigWebhook = await ConfigWebhook . load ( ) ;
136140
@@ -146,22 +150,20 @@ describe('ConfigLoader Tests', () => {
146150 'Failed to load parameter for matcherConfig from path /path/to/matcher/config: Failed to load matcher config' ,
147151 ) ,
148152 ) ;
149- vi . mocked ( getParameter ) . mockResolvedValue ( '' ) ;
153+ githubWebhookSecretStore . get . mockResolvedValue ( '' ) ;
150154
151155 await expect ( ConfigWebhook . load ( ) ) . rejects . toThrow (
152156 'Failed to load config: Failed to load parameter for matcherConfig from path /path/to/matcher/config: Failed to load matcher config' ,
153157 ) ;
154158 } ) ;
155159
156160 it ( 'should load combined matcher config returned by the store' , async ( ) => {
157- process . env . PARAMETER_GITHUB_APP_WEBHOOK_SECRET = '/path/to/webhook/secret' ;
158-
159161 const combinedMatcherConfig = [
160162 { id : '1' , arn : 'arn:aws:sqs:queue1' , matcherConfig : { labelMatchers : [ [ 'a' ] ] , exactMatch : true } } ,
161163 { id : '2' , arn : 'arn:aws:sqs:queue2' , matcherConfig : { labelMatchers : [ [ 'b' ] ] , exactMatch : true } } ,
162164 ] ;
163165 runnerMatcherConfigStore . get . mockResolvedValue ( JSON . stringify ( combinedMatcherConfig ) ) ;
164- vi . mocked ( getParameter ) . mockResolvedValue ( 'secret' ) ;
166+ githubWebhookSecretStore . get . mockResolvedValue ( 'secret' ) ;
165167
166168 const config : ConfigWebhook = await ConfigWebhook . load ( ) ;
167169
@@ -170,13 +172,12 @@ describe('ConfigLoader Tests', () => {
170172 } ) ;
171173
172174 it ( 'should propagate an error from the matcher config store' , async ( ) => {
173- process . env . PARAMETER_GITHUB_APP_WEBHOOK_SECRET = '/path/to/webhook/secret' ;
174175 runnerMatcherConfigStore . get . mockRejectedValue (
175176 new Error (
176177 "Failed to load/parse combined matcher config: Expected ',' or ']' after array element in JSON at position 196" ,
177178 ) ,
178179 ) ;
179- vi . mocked ( getParameter ) . mockResolvedValue ( 'secret' ) ;
180+ githubWebhookSecretStore . get . mockResolvedValue ( 'secret' ) ;
180181
181182 await expect ( ConfigWebhook . load ( ) ) . rejects . toThrow (
182183 "Failed to load config: Failed to load/parse combined matcher config: Expected ',' or ']' after array element in JSON at position 196" ,
@@ -188,14 +189,7 @@ describe('ConfigLoader Tests', () => {
188189 it ( 'should load config successfully' , async ( ) => {
189190 process . env . ACCEPT_EVENTS = '["push", "pull_request"]' ;
190191 process . env . EVENT_BUS_NAME = 'event-bus' ;
191- process . env . PARAMETER_GITHUB_APP_WEBHOOK_SECRET = '/path/to/webhook/secret' ;
192-
193- vi . mocked ( getParameter ) . mockImplementation ( async ( paramPath : string ) => {
194- if ( paramPath === '/path/to/webhook/secret' ) {
195- return 'secret' ;
196- }
197- return '' ;
198- } ) ;
192+ githubWebhookSecretStore . get . mockResolvedValue ( 'secret' ) ;
199193
200194 const config : ConfigWebhookEventBridge = await ConfigWebhookEventBridge . load ( ) ;
201195
@@ -206,13 +200,23 @@ describe('ConfigLoader Tests', () => {
206200 } ) ;
207201
208202 it ( 'should throw error if config loading fails' , async ( ) => {
209- vi . mocked ( getParameter ) . mockImplementation ( async ( paramPath : string ) => {
210- throw new Error ( `Parameter ${ paramPath } not found` ) ;
203+ githubWebhookSecretStore . get . mockRejectedValue ( new Error ( 'Webhook secret store is unavailable' ) ) ;
204+
205+ await expect ( ConfigWebhookEventBridge . load ( ) ) . rejects . toThrow (
206+ 'Failed to load config: Environment variable for eventBusName is not set and no default value provided., Webhook secret store is unavailable' ,
207+ ) ;
208+ } ) ;
209+
210+ it ( 'should report an error selecting the webhook secret store' , async ( ) => {
211+ process . env . EVENT_BUS_NAME = 'event-bus' ;
212+ vi . mocked ( getGitHubWebhookSecretStore ) . mockImplementationOnce ( ( ) => {
213+ throw new Error ( "Unsupported runner config storage provider 'not-registered'" ) ;
211214 } ) ;
212215
213216 await expect ( ConfigWebhookEventBridge . load ( ) ) . rejects . toThrow (
214- ' Failed to load config: Environment variable for eventBusName is not set and no default value provided., Failed to load parameter for webhookSecret from path undefined: Parameter undefined not found' ,
217+ " Failed to load config: Unsupported runner config storage provider ' not-registered'" ,
215218 ) ;
219+ expect ( githubWebhookSecretStore . get ) . not . toHaveBeenCalled ( ) ;
216220 } ) ;
217221 } ) ;
218222
0 commit comments