1
1
// camel-k: language=java
2
2
3
+ import java .io .IOException ;
4
+ import java .io .StringReader ;
3
5
import java .security .GeneralSecurityException ;
6
+ import java .security .KeyPair ;
4
7
import java .security .MessageDigest ;
5
8
import java .security .SignatureException ;
9
+ import java .security .interfaces .RSAPrivateKey ;
6
10
import java .util .Base64 ;
7
11
import java .util .Base64 .Decoder ;
8
12
import java .util .Map ;
9
13
10
14
import javax .crypto .Mac ;
11
15
import javax .crypto .spec .SecretKeySpec ;
12
16
17
+ import com .auth0 .jwt .JWT ;
18
+ import com .auth0 .jwt .algorithms .Algorithm ;
19
+ import com .auth0 .jwt .exceptions .JWTCreationException ;
20
+
13
21
import org .apache .camel .Exchange ;
14
22
import org .apache .camel .ExchangeProperty ;
15
23
import org .apache .camel .Header ;
18
26
import org .apache .camel .support .builder .PredicateBuilder ;
19
27
import org .apache .commons .codec .DecoderException ;
20
28
import org .apache .commons .codec .binary .Hex ;
29
+ import org .bouncycastle .openssl .PEMKeyPair ;
30
+ import org .bouncycastle .openssl .PEMParser ;
31
+ import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
21
32
22
33
import io .fabric8 .kubernetes .api .model .Secret ;
23
34
@@ -49,6 +60,28 @@ void validateSignature(
49
60
log .info ("-> Signature {} is valid" , signature );
50
61
}
51
62
63
+ String generateJWT (
64
+ @ ExchangeProperty (GitHubApp .PEM ) String pem ,
65
+ @ ExchangeProperty ("app_id" ) String appId )
66
+ throws IOException {
67
+
68
+ try (PEMParser parser = new PEMParser (new StringReader (pem ))) {
69
+
70
+ // read the private key
71
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter ();
72
+ KeyPair kp = converter .getKeyPair ((PEMKeyPair ) parser .readObject ());
73
+ RSAPrivateKey key = (RSAPrivateKey ) kp .getPrivate ();
74
+
75
+ // create and sign JWT with private key
76
+ return JWT .create ()
77
+ .withIssuer (appId )
78
+ .sign (Algorithm .RSA256 (null , key ));
79
+ }
80
+ catch (JWTCreationException e ) {
81
+ throw new IOException ("Error creating JWT token" , e );
82
+ }
83
+ }
84
+
52
85
@ Override
53
86
public void configure () throws Exception {
54
87
@@ -77,6 +110,7 @@ public void configure() throws Exception {
77
110
78
111
.onException (DecoderException .class )
79
112
.onException (GeneralSecurityException .class )
113
+ .onException (IOException .class )
80
114
.handled (true )
81
115
.removeHeaders ("*" )
82
116
.setHeader (Exchange .HTTP_RESPONSE_CODE , constant (404 ))
@@ -90,9 +124,10 @@ public void configure() throws Exception {
90
124
// get app secrets
91
125
.to ("direct:get-secret" )
92
126
127
+ // validate the event and request for an access token
93
128
.bean (this , "validateSignature" )
94
- // TODO: generate JWT
95
- // TODO: request access token
129
+ . setProperty ( "jwt" , method ( this , "generateJWT" ))
130
+ . log ( "TODO: ${exchangeProperty[jwt]}" ) // TODO: request access token
96
131
97
132
// restore original payload
98
133
.setBody (exchangeProperty ("payload" ))
@@ -113,6 +148,7 @@ public void configure() throws Exception {
113
148
Map <String , String > data = secret .getData ();
114
149
115
150
exchange .setProperty (GitHubApp .WEBHOOK_SECRET , decoder .decode (data .get (GitHubApp .WEBHOOK_SECRET )));
151
+ exchange .setProperty (GitHubApp .PEM , decoder .decode (data .get (GitHubApp .PEM )));
116
152
}
117
153
});
118
154
}
0 commit comments