|
11 | 11 | import java.security.spec.PKCS8EncodedKeySpec;
|
12 | 12 | import java.security.spec.X509EncodedKeySpec;
|
13 | 13 | import java.util.Arrays;
|
| 14 | +import java.util.Base64; |
14 | 15 | import java.util.Date;
|
15 | 16 | import java.util.List;
|
16 | 17 |
|
17 |
| -import javax.xml.bind.DatatypeConverter; |
18 |
| - |
19 | 18 | import org.apache.commons.lang3.StringUtils;
|
20 | 19 |
|
21 | 20 | import com.auth0.jwt.JWT;
|
22 | 21 | import com.auth0.jwt.JWTCreator.Builder;
|
23 | 22 | import com.auth0.jwt.algorithms.Algorithm;
|
24 | 23 |
|
| 24 | +/** |
| 25 | + * This class provides a small example application to generate JWT tokens for |
| 26 | + * the dynamic log level feature. The input data is hard-coded in the |
| 27 | + * source-code. Please change the value for your use-case. |
| 28 | + * |
| 29 | + */ |
25 | 30 | public class TokenCreator {
|
26 | 31 |
|
27 |
| - private static final List<String> ALLOWED_DYNAMIC_LOGLEVELS = Arrays.asList("TRACE", "DEBUG", "INFO", "WARN", |
28 |
| - "ERROR"); |
29 |
| - |
30 |
| - /** |
31 |
| - * Run this application locally in order to generate valid dynamic log level JWT |
32 |
| - * tokens which enable you to change the log level threshold on your |
33 |
| - * CF-Application for a single thread. |
34 |
| - */ |
35 |
| - public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, |
36 |
| - DynamicLogLevelException, InvalidKeySpecException { |
37 |
| - |
38 |
| - /* |
39 |
| - * PLEASE PROVIDE THIS INFORMATION *********************************** |
40 |
| - */ |
41 |
| - // Replace with email address |
42 |
| - String issuer = "[email protected]"; |
43 |
| - // Replace with the log level that should be transmitted via the token |
44 |
| - // Valid log level thresholds are: |
45 |
| - // "TRACE", "DEBUG", "INFO", "WARN", "ERROR" |
46 |
| - String level = "TRACE"; |
47 |
| - // Replace with the packages that should be transmitted via the token |
48 |
| - // Multiple packages should be separated by a comma. |
49 |
| - String packages = ""; |
50 |
| - // Set a validity period in days |
51 |
| - long validityPeriodInDays = 2; |
52 |
| - // If available provide Base64 encoded private key here: |
53 |
| - String privateKey = ""; |
54 |
| - // If available provide Base64 encoded private key here: |
55 |
| - String publicKey = ""; |
56 |
| - // (If no keys are provided, new keys will be generated) |
57 |
| - /* |
58 |
| - * ******************************************************************** |
59 |
| - */ |
60 |
| - |
61 |
| - KeyPair keyPair; |
62 |
| - |
63 |
| - if (StringUtils.isNotBlank(privateKey)) { |
64 |
| - keyPair = new KeyPair(publicKeyConverter(publicKey), privateKeyConverter(privateKey)); |
65 |
| - } |
66 |
| - |
67 |
| - else { |
68 |
| - KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); |
69 |
| - keyGen.initialize(2048); |
70 |
| - keyPair = keyGen.generateKeyPair(); |
71 |
| - // keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
72 |
| - } |
73 |
| - privateKey = DatatypeConverter.printBase64Binary(keyPair.getPrivate().getEncoded()); |
74 |
| - publicKey = DatatypeConverter.printBase64Binary(keyPair.getPublic().getEncoded()); |
75 |
| - Date issuedAt = new Date(); |
76 |
| - Date expiresAt = new Date(new Date().getTime() + validityPeriodInDays * 86400000); |
77 |
| - String token = TokenCreator.createToken(keyPair, issuer, issuedAt, expiresAt, level, packages); |
78 |
| - |
79 |
| - System.out.println("You successfully created a dynamic log level token with log level " + level |
80 |
| - + " and packages " + packages + "!"); |
81 |
| - System.out.println(); |
82 |
| - System.out.println("Your private key is:"); |
83 |
| - System.out.println(privateKey); |
84 |
| - System.out.println("Your public key is:"); |
85 |
| - System.out.println(publicKey); |
86 |
| - System.out.println("Your JWT token with log level " + level + " is:"); |
87 |
| - System.out.println(token); |
88 |
| - System.out.println(); |
89 |
| - System.out.println("Please copy and save token and keys for later usage. The JWT token can now be written"); |
90 |
| - System.out.println("to an HTTP header in order to change the corresponding request's log level to " + level); |
91 |
| - System.out.println("For token validation, the public key must be added to the environment of the application."); |
92 |
| - System.out |
93 |
| - .println("In order to generate a new token with specific keys, the variables privateKey and publicKey"); |
94 |
| - System.out.println("can be instantiated with these keys"); |
95 |
| - |
96 |
| - } |
97 |
| - |
98 |
| - public static String createToken(KeyPair keyPair, String issuer, Date issuedAt, Date expiresAt, String level, |
99 |
| - String packages) throws NoSuchAlgorithmException, NoSuchProviderException, DynamicLogLevelException { |
100 |
| - Algorithm rsa256 = Algorithm.RSA256((RSAPublicKey) keyPair.getPublic(), (RSAPrivateKey) keyPair.getPrivate()); |
101 |
| - if (ALLOWED_DYNAMIC_LOGLEVELS.contains(level)) { |
102 |
| - Builder builder = JWT.create().withIssuer(issuer).// |
103 |
| - withIssuedAt(issuedAt). // |
104 |
| - withExpiresAt(expiresAt).// |
105 |
| - withClaim("level", level); |
106 |
| - builder = StringUtils.isNotBlank(packages) ? builder.withClaim("packages", packages) : builder; |
107 |
| - return builder.withClaim("packages", packages).sign(rsa256); |
108 |
| - } else { |
109 |
| - throw new DynamicLogLevelException("Dynamic Log-Level [" + level |
110 |
| - + "] provided in header is not valid. Allowed Values are " + ALLOWED_DYNAMIC_LOGLEVELS.toString()); |
111 |
| - } |
112 |
| - } |
113 |
| - |
114 |
| - private static RSAPublicKey publicKeyConverter(String pemKey) |
115 |
| - throws NoSuchAlgorithmException, InvalidKeySpecException { |
116 |
| - byte[] keyBytes = DatatypeConverter.parseBase64Binary(pemKey); |
117 |
| - X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); |
118 |
| - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
119 |
| - return (RSAPublicKey) keyFactory.generatePublic(spec); |
120 |
| - } |
121 |
| - |
122 |
| - private static RSAPrivateKey privateKeyConverter(String pemKey) |
123 |
| - throws NoSuchAlgorithmException, InvalidKeySpecException { |
124 |
| - byte[] keyBytes = DatatypeConverter.parseBase64Binary(pemKey); |
125 |
| - PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); |
126 |
| - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
127 |
| - return (RSAPrivateKey) keyFactory.generatePrivate(spec); |
128 |
| - } |
| 32 | + private static final List<String> ALLOWED_DYNAMIC_LOGLEVELS = Arrays.asList("TRACE", "DEBUG", "INFO", "WARN", |
| 33 | + "ERROR"); |
| 34 | + |
| 35 | + /** |
| 36 | + * Run this application locally in order to generate valid dynamic log level |
| 37 | + * JWT tokens which enable you to change the log level threshold on your |
| 38 | + * CF-Application for a single thread. |
| 39 | + */ |
| 40 | + public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, |
| 41 | + DynamicLogLevelException, InvalidKeySpecException { |
| 42 | + |
| 43 | + /* |
| 44 | + * PLEASE PROVIDE THIS INFORMATION *********************************** |
| 45 | + */ |
| 46 | + // Replace with email address |
| 47 | + String issuer = "[email protected]"; |
| 48 | + // Replace with the log level that should be transmitted via the token |
| 49 | + // Valid log level thresholds are: |
| 50 | + // "TRACE", "DEBUG", "INFO", "WARN", "ERROR" |
| 51 | + String level = "TRACE"; |
| 52 | + // Replace with the packages that should be transmitted via the token |
| 53 | + // Multiple packages should be separated by a comma. |
| 54 | + String packages = ""; |
| 55 | + // Set a validity period in days |
| 56 | + long validityPeriodInDays = 2; |
| 57 | + // If available provide Base64 encoded private key here: |
| 58 | + String privateKey = ""; |
| 59 | + // If available provide Base64 encoded private key here: |
| 60 | + String publicKey = ""; |
| 61 | + // (If no keys are provided, new keys will be generated) |
| 62 | + /* |
| 63 | + * ******************************************************************** |
| 64 | + */ |
| 65 | + |
| 66 | + KeyPair keyPair; |
| 67 | + |
| 68 | + if (StringUtils.isNotBlank(privateKey)) { |
| 69 | + keyPair = new KeyPair(publicKeyConverter(publicKey), privateKeyConverter(privateKey)); |
| 70 | + } |
| 71 | + |
| 72 | + else { |
| 73 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); |
| 74 | + keyGen.initialize(2048); |
| 75 | + keyPair = keyGen.generateKeyPair(); |
| 76 | + // keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
| 77 | + } |
| 78 | + privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()); |
| 79 | + publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()); |
| 80 | + Date issuedAt = new Date(); |
| 81 | + Date expiresAt = new Date(new Date().getTime() + validityPeriodInDays * 86400000); |
| 82 | + String token = TokenCreator.createToken(keyPair, issuer, issuedAt, expiresAt, level, packages); |
| 83 | + |
| 84 | + System.out.println("You successfully created a dynamic log level token with log level " + level + |
| 85 | + " and packages " + packages + "!"); |
| 86 | + System.out.println(); |
| 87 | + System.out.println("Your private key is:"); |
| 88 | + System.out.println(privateKey); |
| 89 | + System.out.println("Your public key is:"); |
| 90 | + System.out.println(publicKey); |
| 91 | + System.out.println("Your JWT token with log level " + level + " is:"); |
| 92 | + System.out.println(token); |
| 93 | + System.out.println(); |
| 94 | + System.out.println("Please copy and save token and keys for later usage. The JWT token can now be written"); |
| 95 | + System.out.println("to an HTTP header in order to change the corresponding request's log level to " + level); |
| 96 | + System.out.println("For token validation, the public key must be added to the environment of the application."); |
| 97 | + System.out.println("In order to generate a new token with specific keys, the variables privateKey and publicKey"); |
| 98 | + System.out.println("can be instantiated with these keys"); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + public static String createToken(KeyPair keyPair, String issuer, Date issuedAt, Date expiresAt, String level, |
| 103 | + String packages) throws NoSuchAlgorithmException, NoSuchProviderException, |
| 104 | + DynamicLogLevelException { |
| 105 | + Algorithm rsa256 = Algorithm.RSA256((RSAPublicKey) keyPair.getPublic(), (RSAPrivateKey) keyPair.getPrivate()); |
| 106 | + if (ALLOWED_DYNAMIC_LOGLEVELS.contains(level)) { |
| 107 | + Builder builder = JWT.create().withIssuer(issuer).// |
| 108 | + withIssuedAt(issuedAt). // |
| 109 | + withExpiresAt(expiresAt).// |
| 110 | + withClaim("level", level); |
| 111 | + builder = StringUtils.isNotBlank(packages) ? builder.withClaim("packages", packages) : builder; |
| 112 | + return builder.withClaim("packages", packages).sign(rsa256); |
| 113 | + } else { |
| 114 | + throw new DynamicLogLevelException("Dynamic Log-Level [" + level + |
| 115 | + "] provided in header is not valid. Allowed Values are " + |
| 116 | + ALLOWED_DYNAMIC_LOGLEVELS.toString()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static RSAPublicKey publicKeyConverter(String pemKey) throws NoSuchAlgorithmException, |
| 121 | + InvalidKeySpecException { |
| 122 | + byte[] keyBytes = Base64.getDecoder().decode(pemKey); |
| 123 | + X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); |
| 124 | + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 125 | + return (RSAPublicKey) keyFactory.generatePublic(spec); |
| 126 | + } |
| 127 | + |
| 128 | + private static RSAPrivateKey privateKeyConverter(String pemKey) throws NoSuchAlgorithmException, |
| 129 | + InvalidKeySpecException { |
| 130 | + byte[] keyBytes = Base64.getDecoder().decode(pemKey); |
| 131 | + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); |
| 132 | + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 133 | + return (RSAPrivateKey) keyFactory.generatePrivate(spec); |
| 134 | + } |
129 | 135 |
|
130 | 136 | }
|
0 commit comments