11package io .quarkiverse .githubapp .runtime .config ;
22
33import java .security .PrivateKey ;
4+ import java .util .Map ;
45import java .util .Optional ;
56import java .util .Set ;
67import java .util .TreeSet ;
1112import org .jboss .logging .Logger ;
1213
1314import io .quarkiverse .githubapp .ConfigFile ;
15+ import io .quarkiverse .githubapp .Credentials ;
1416import io .quarkiverse .githubapp .GitHubEvent ;
1517import io .quarkiverse .githubapp .runtime .config .GitHubAppRuntimeConfig .Debug ;
18+ import io .quarkus .arc .Arc ;
19+ import io .quarkus .arc .ArcContainer ;
20+ import io .quarkus .credentials .CredentialsProvider ;
1621import io .quarkus .runtime .LaunchMode ;
1722import io .quarkus .runtime .Startup ;
1823
@@ -26,28 +31,45 @@ public class CheckedConfigProvider {
2631
2732 private final LaunchMode launchMode ;
2833
34+ private final Optional <PrivateKey > privateKey ;
35+ private final Optional <String > webhookSecret ;
36+
2937 private final Set <String > missingPropertyKeys = new TreeSet <>();
3038
3139 @ Inject
3240 CheckedConfigProvider (GitHubAppRuntimeConfig gitHubAppRuntimeConfig , LaunchMode launchMode ) {
3341 this .gitHubAppRuntimeConfig = gitHubAppRuntimeConfig ;
3442 this .launchMode = launchMode ;
3543
44+ Map <String , String > credentials = getCredentials ();
45+ String privateKeyFromCredentials = credentials .get (Credentials .PRIVATE_KEY );
46+ if (privateKeyFromCredentials != null && !privateKeyFromCredentials .isBlank ()) {
47+ this .privateKey = Optional .of (new PrivateKeyConverter ().convert (privateKeyFromCredentials .trim ()));
48+ } else {
49+ this .privateKey = gitHubAppRuntimeConfig .privateKey ;
50+ }
51+ String webhookSecretFromCredentials = credentials .get (Credentials .WEBHOOK_SECRET );
52+ if (webhookSecretFromCredentials != null && !webhookSecretFromCredentials .isBlank ()) {
53+ this .webhookSecret = Optional .of (webhookSecretFromCredentials .trim ());
54+ } else {
55+ this .webhookSecret = gitHubAppRuntimeConfig .webhookSecret ;
56+ }
57+
3658 if (gitHubAppRuntimeConfig .appId .isEmpty ()) {
3759 missingPropertyKeys .add ("quarkus.github-app.app-id (.env: QUARKUS_GITHUB_APP_APP_ID)" );
3860 }
39- if (gitHubAppRuntimeConfig .privateKey .isEmpty ()) {
61+ if (this .privateKey .isEmpty ()) {
4062 missingPropertyKeys .add ("quarkus.github-app.private-key (.env: QUARKUS_GITHUB_APP_PRIVATE_KEY)" );
4163 }
42- if (launchMode == LaunchMode .NORMAL && gitHubAppRuntimeConfig .webhookSecret .isEmpty ()) {
64+ if (launchMode == LaunchMode .NORMAL && this .webhookSecret .isEmpty ()) {
4365 missingPropertyKeys .add ("quarkus.github-app.webhook-secret (.env: QUARKUS_GITHUB_APP_WEBHOOK_SECRET)" );
4466 }
4567
4668 if (launchMode != LaunchMode .TEST ) {
4769 checkConfig ();
4870 }
4971
50- if (gitHubAppRuntimeConfig .webhookSecret .isPresent () && launchMode .isDevOrTest ()) {
72+ if (this .webhookSecret .isPresent () && launchMode .isDevOrTest ()) {
5173 LOG .info ("Payload signature checking is disabled in dev and test modes." );
5274 }
5375 }
@@ -71,11 +93,11 @@ public PrivateKey privateKey() {
7193 }
7294
7395 // The optional will never be empty; using orElseThrow instead of get to avoid IDE warnings.
74- return gitHubAppRuntimeConfig . privateKey .orElseThrow ();
96+ return privateKey .orElseThrow ();
7597 }
7698
7799 public Optional <String > webhookSecret () {
78- return gitHubAppRuntimeConfig . webhookSecret ;
100+ return webhookSecret ;
79101 }
80102
81103 public Optional <String > webhookProxyUrl () {
@@ -124,4 +146,29 @@ public void checkConfig() {
124146
125147 throw new GitHubAppConfigurationException (errorMessage );
126148 }
149+
150+ private Map <String , String > getCredentials () {
151+ if (gitHubAppRuntimeConfig .credentialsProvider .isEmpty ()) {
152+ return Map .of ();
153+ }
154+
155+ String beanName = gitHubAppRuntimeConfig .credentialsProviderName .orElse (null );
156+ CredentialsProvider credentialsProvider = getCredentialsProvider (beanName );
157+ String keyRingName = gitHubAppRuntimeConfig .credentialsProvider .get ();
158+
159+ return credentialsProvider .getCredentials (keyRingName );
160+ }
161+
162+ private static CredentialsProvider getCredentialsProvider (String name ) {
163+ ArcContainer container = Arc .container ();
164+ CredentialsProvider credentialsProvider = name != null
165+ ? (CredentialsProvider ) container .instance (name ).get ()
166+ : container .instance (CredentialsProvider .class ).get ();
167+
168+ if (credentialsProvider == null ) {
169+ throw new RuntimeException ("Unable to find credentials provider of name " + (name == null ? "default" : name ));
170+ }
171+
172+ return credentialsProvider ;
173+ }
127174}
0 commit comments