Skip to content

Commit aecdfac

Browse files
authored
expose jag api functions in zts java client (#3119)
* expose jag api functions in zts java client Signed-off-by: Henry Avetisyan <hga@yahooinc.com> * generate id token request builder object as well Signed-off-by: Henry Avetisyan <hga@yahooinc.com> --------- Signed-off-by: Henry Avetisyan <hga@yahooinc.com>
1 parent abc9612 commit aecdfac

File tree

18 files changed

+4742
-197
lines changed

18 files changed

+4742
-197
lines changed

clients/java/msd/src/main/java/com/yahoo/athenz/msd/MSDRDLGeneratedClient.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ public TransportPolicyRules getTransportPolicyRules(String matchingTag, java.uti
206206
case 200:
207207
case 304:
208208
if (headers != null) {
209-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
209+
if (httpResponse.getFirstHeader("ETag") != null) {
210+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
211+
}
210212
}
211213
if (code == 304) {
212214
return null;
@@ -301,7 +303,9 @@ public TransportPolicyRules getTransportPolicyRulesByDomain(String domainName, S
301303
case 200:
302304
case 304:
303305
if (headers != null) {
304-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
306+
if (httpResponse.getFirstHeader("ETag") != null) {
307+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
308+
}
305309
}
306310
if (code == 304) {
307311
return null;
@@ -381,7 +385,9 @@ public TransportPolicyRules getTransportPolicyRulesByService(String domainName,
381385
case 200:
382386
case 304:
383387
if (headers != null) {
384-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
388+
if (httpResponse.getFirstHeader("ETag") != null) {
389+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
390+
}
385391
}
386392
if (code == 304) {
387393
return null;
@@ -456,7 +462,9 @@ public Workloads getWorkloadsByService(String domainName, String serviceName, St
456462
case 200:
457463
case 304:
458464
if (headers != null) {
459-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
465+
if (httpResponse.getFirstHeader("ETag") != null) {
466+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
467+
}
460468
}
461469
if (code == 304) {
462470
return null;
@@ -494,7 +502,9 @@ public Workloads getWorkloadsByIP(String ip, String matchingTag, java.util.Map<S
494502
case 200:
495503
case 304:
496504
if (headers != null) {
497-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
505+
if (httpResponse.getFirstHeader("ETag") != null) {
506+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
507+
}
498508
}
499509
if (code == 304) {
500510
return null;
@@ -701,7 +711,9 @@ public Workloads getWorkloadsByDomain(String domainName, String matchingTag, jav
701711
case 200:
702712
case 304:
703713
if (headers != null) {
704-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
714+
if (httpResponse.getFirstHeader("ETag") != null) {
715+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
716+
}
705717
}
706718
if (code == 304) {
707719
return null;
@@ -740,7 +752,9 @@ public BulkWorkloadResponse getWorkloadsByDomainAndService(BulkWorkloadRequest r
740752
case 200:
741753
case 304:
742754
if (headers != null) {
743-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
755+
if (httpResponse.getFirstHeader("ETag") != null) {
756+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
757+
}
744758
}
745759
if (code == 304) {
746760
return null;
@@ -935,7 +949,9 @@ public KubernetesNetworkPolicyResponse postKubernetesNetworkPolicyRequest(String
935949
case 200:
936950
case 304:
937951
if (headers != null) {
938-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
952+
if (httpResponse.getFirstHeader("ETag") != null) {
953+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
954+
}
939955
}
940956
if (code == 304) {
941957
return null;

clients/java/zms/src/main/java/com/yahoo/athenz/zms/ZMSRDLGeneratedClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,7 +3795,9 @@ public SignedDomains getSignedDomains(String domain, String metaOnly, String met
37953795
case 200:
37963796
case 304:
37973797
if (headers != null) {
3798-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
3798+
if (httpResponse.getFirstHeader("ETag") != null) {
3799+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
3800+
}
37993801
}
38003802
if (code == 304) {
38013803
return null;
@@ -3836,7 +3838,9 @@ public JWSDomain getJWSDomain(String name, Boolean signatureP1363Format, String
38363838
case 200:
38373839
case 304:
38383840
if (headers != null) {
3839-
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
3841+
if (httpResponse.getFirstHeader("ETag") != null) {
3842+
headers.put("tag", List.of(httpResponse.getFirstHeader("ETag").getValue()));
3843+
}
38403844
}
38413845
if (code == 304) {
38423846
return null;

clients/java/zts/examples/tls-support/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
<properties>
2828
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2929
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
30-
<athenz.version>1.12.20</athenz.version>
30+
<athenz.version>1.12.29</athenz.version>
3131
<jjwt.version>0.11.5</jjwt.version>
32-
<nimbus.version>10.3.1</nimbus.version>
32+
<nimbus.version>10.6</nimbus.version>
3333
</properties>
3434

3535
<dependencies>
@@ -68,7 +68,7 @@
6868
<dependency>
6969
<groupId>commons-cli</groupId>
7070
<artifactId>commons-cli</artifactId>
71-
<version>1.9.0</version>
71+
<version>1.10.0</version>
7272
</dependency>
7373
<dependency>
7474
<groupId>org.slf4j</groupId>
@@ -78,7 +78,7 @@
7878
<dependency>
7979
<groupId>ch.qos.logback</groupId>
8080
<artifactId>logback-classic</artifactId>
81-
<version>1.5.18</version>
81+
<version>1.5.20</version>
8282
</dependency>
8383
</dependencies>
8484

@@ -87,7 +87,7 @@
8787
<plugin>
8888
<groupId>org.apache.maven.plugins</groupId>
8989
<artifactId>maven-compiler-plugin</artifactId>
90-
<version>3.14.0</version>
90+
<version>3.14.1</version>
9191
<configuration>
9292
<source>11</source>
9393
<target>11</target>
@@ -96,7 +96,7 @@
9696
<plugin>
9797
<groupId>org.apache.maven.plugins</groupId>
9898
<artifactId>maven-dependency-plugin</artifactId>
99-
<version>3.8.1</version>
99+
<version>3.9.0</version>
100100
<executions>
101101
<execution>
102102
<id>copy</id>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
* Copyright The Athenz Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.yahoo.athenz.example.zts.tls.client;
17+
18+
import javax.net.ssl.SSLContext;
19+
20+
import com.yahoo.athenz.zts.*;
21+
import com.oath.auth.KeyRefresher;
22+
import com.oath.auth.Utils;
23+
import org.apache.commons.cli.*;
24+
25+
import java.util.Collections;
26+
27+
public class ZTSTLSClientJAGTokenExchange {
28+
29+
public static void main(String[] args) {
30+
31+
// parse our command line to retrieve required input
32+
33+
CommandLine cmd = parseCommandLine(args);
34+
35+
final String domainName = cmd.getOptionValue("domain").toLowerCase();
36+
final String ztsUrl = cmd.getOptionValue("ztsurl");
37+
final String idKeyPath = cmd.getOptionValue("idKey");
38+
final String idCertPath = cmd.getOptionValue("idCert");
39+
final String trustStorePath = cmd.getOptionValue("trustStorePath");
40+
final String trustStorePassword = cmd.getOptionValue("trustStorePassword");
41+
final String clientId = cmd.getOptionValue("clientId");
42+
final String expiryTime = cmd.getOptionValue("expiryTime");
43+
final String roleName = cmd.getOptionValue("roleName");
44+
final String resource = cmd.getOptionValue("resource");
45+
final String svcKeyPath = cmd.getOptionValue("svcKey");
46+
final String svcCertPath = cmd.getOptionValue("svcCert");
47+
48+
// we are going to set up our service private key and
49+
// certificate into a ssl context that we can use with
50+
// our zts client
51+
52+
try {
53+
KeyRefresher keyRefresher = Utils.generateKeyRefresher(trustStorePath, trustStorePassword,
54+
idCertPath, idKeyPath);
55+
SSLContext sslContext = Utils.buildSSLContext(keyRefresher.getKeyManagerProxy(),
56+
keyRefresher.getTrustManagerProxy());
57+
58+
String idToken = null;
59+
try (ZTSClient ztsClient = new ZTSClient(ztsUrl, sslContext)) {
60+
61+
// first we need to request an id token for the user
62+
63+
IDTokenRequestBuilder builder = IDTokenRequestBuilder.newBuilder(IDTokenRequestBuilder.OPENID_RESPONSE_TYPE_ID_TOKEN)
64+
.clientId(clientId)
65+
.scope("openid")
66+
.keyType("EC")
67+
.expiryTime(Integer.parseInt(expiryTime));
68+
69+
OIDCResponse response = ztsClient.getIDToken(builder, false);
70+
71+
idToken = response.getId_token();
72+
System.out.println("Our ID Token is:");
73+
System.out.println(idToken);
74+
75+
} catch (ZTSClientException ex) {
76+
System.out.println("Unable to retrieve id token: " + ex.getMessage());
77+
System.exit(2);
78+
}
79+
80+
KeyRefresher keyRefresherJag = Utils.generateKeyRefresher(trustStorePath, trustStorePassword,
81+
svcCertPath, svcKeyPath);
82+
SSLContext sslContextJag = Utils.buildSSLContext(keyRefresherJag.getKeyManagerProxy(),
83+
keyRefresherJag.getTrustManagerProxy());
84+
85+
// now we need to get our jag token
86+
87+
try (ZTSClient ztsClient = new ZTSClient(ztsUrl, sslContextJag)) {
88+
89+
// generate our jag token request
90+
91+
OAuthTokenRequestBuilder builder = OAuthTokenRequestBuilder.newBuilder(OAuthTokenRequestBuilder.OAUTH_GRANT_TOKEN_EXCHANGE)
92+
.domainName(domainName)
93+
.requestedTokenType(OAuthTokenRequestBuilder.OAUTH_TOKEN_TYPE_JAG)
94+
.audience(ztsUrl)
95+
.resource(resource)
96+
.roleNames(Collections.singletonList(roleName))
97+
.subjectTokenType(OAuthTokenRequestBuilder.OAUTH_TOKEN_TYPE_ID)
98+
.subjectToken(idToken);
99+
100+
AccessTokenResponse tokenResponse = ztsClient.getJAGToken(builder);
101+
102+
String jagToken = tokenResponse.getAccess_token();
103+
System.out.println("JAG Token is:");
104+
System.out.println(jagToken);
105+
106+
107+
builder = OAuthTokenRequestBuilder.newBuilder(OAuthTokenRequestBuilder.OAUTH_GRANT_JWT_BEARER)
108+
.assertion(jagToken);
109+
110+
tokenResponse = ztsClient.getJAGExchangeToken(builder);
111+
System.out.println("JAG Exchange Token is:");
112+
System.out.println(tokenResponse.getAccess_token());
113+
114+
} catch (ZTSClientException ex) {
115+
System.out.println("Unable to retrieve jag exchange token: " + ex.getMessage());
116+
System.exit(2);
117+
}
118+
} catch (Exception ex) {
119+
System.out.println("Exception: " + ex.getMessage());
120+
ex.printStackTrace();
121+
System.exit(1);
122+
}
123+
}
124+
125+
private static CommandLine parseCommandLine(String[] args) {
126+
127+
Options options = new Options();
128+
129+
Option domain = new Option("d", "domain", true, "domain name");
130+
domain.setRequired(true);
131+
options.addOption(domain);
132+
133+
Option idKey = new Option("k", "idKey", true, "private key path for id token");
134+
idKey.setRequired(true);
135+
options.addOption(idKey);
136+
137+
Option idCert = new Option("c", "idCert", true, "certificate path for id token");
138+
idCert.setRequired(true);
139+
options.addOption(idCert);
140+
141+
Option trustStore = new Option("t", "trustStorePath", true, "CA TrustStore path");
142+
trustStore.setRequired(true);
143+
options.addOption(trustStore);
144+
145+
Option trustStorePassword = new Option("p", "trustStorePassword", true, "CA TrustStore password");
146+
trustStorePassword.setRequired(true);
147+
options.addOption(trustStorePassword);
148+
149+
Option ztsUrl = new Option("z", "ztsurl", true, "ZTS Server url");
150+
ztsUrl.setRequired(true);
151+
options.addOption(ztsUrl);
152+
153+
Option clientId = new Option("s", "clientId", true, "ID Token client ID");
154+
clientId.setRequired(true);
155+
options.addOption(clientId);
156+
157+
Option roleName = new Option("r", "roleName", true, "Role name for access token");
158+
roleName.setRequired(true);
159+
options.addOption(roleName);
160+
161+
Option resource = new Option("h", "resource", true, "resource object");
162+
resource.setRequired(true);
163+
options.addOption(resource);
164+
165+
Option expiryTime = new Option("e", "expiryTime", true, "Expiry Time in seconds");
166+
expiryTime.setRequired(true);
167+
options.addOption(expiryTime);
168+
169+
Option svcKey = new Option("v", "svcKey", true, "private key path for service token");
170+
svcKey.setRequired(true);
171+
options.addOption(svcKey);
172+
173+
Option svcCert = new Option("w", "svcCert", true, "certificate path for service token");
174+
svcCert.setRequired(true);
175+
options.addOption(svcCert);
176+
177+
CommandLineParser parser = new DefaultParser();
178+
HelpFormatter formatter = new HelpFormatter();
179+
CommandLine cmd = null;
180+
181+
try {
182+
cmd = parser.parse(options, args);
183+
} catch (ParseException e) {
184+
System.out.println(e.getMessage());
185+
formatter.printHelp("zts-access-token-client", options);
186+
System.exit(1);
187+
}
188+
189+
return cmd;
190+
}
191+
}

0 commit comments

Comments
 (0)