Skip to content

Commit 79da0c4

Browse files
committed
Pushes fields from VaultResponse down to the specific response subclass where they're used
1 parent 0f491f4 commit 79da0c4

File tree

4 files changed

+37
-84
lines changed

4 files changed

+37
-84
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ should represent non-breaking changes. The third number represents any very min
138138
* Removes the `com.bettercloud.vault.api.Sys` class, deprecated in the 1.2.0 release.
139139
* Removes the `com.bettercloud.vault.api.Auth.loginByUsernamePassword` method, deprecated in the 1.2.0 release.
140140
* Changes the `com.bettercloud.vault.response.AuthReponse` class field `authLeaseDuration` from type `int` to `long`.
141+
* Removes the fields `leaseId`, `leaseDuration`, and `renewable` from the `VaultResponse` base class, instead
142+
including them only in the subclasses for specific response types where they are used.
141143
* Adds support for authentication via the AppRole auth backend.
142144
* Adds support for renewing secret leases.
143145
* **1.2.0**: This is a substantial release, with numerous additions. It's a minor version number only because there

src/main/java/com/bettercloud/vault/response/AuthResponse.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
public class AuthResponse extends VaultResponse {
1818

19+
private Boolean renewable;
1920
private String authClientToken;
2021
private List<String> authPolicies;
2122
private long authLeaseDuration;
@@ -38,6 +39,7 @@ public AuthResponse(final RestResponse restResponse, final int retries) {
3839
final JsonObject jsonObject = Json.parse(responseJson).asObject();
3940
final JsonObject authJsonObject = jsonObject.get("auth").asObject();
4041

42+
renewable = jsonObject.get("renewable").asBoolean();
4143
authLeaseDuration = authJsonObject.getInt("lease_duration", 0);
4244
authRenewable = authJsonObject.getBoolean("renewable", false);
4345
if (authJsonObject.get("metadata") != null && !authJsonObject.get("metadata").toString().equalsIgnoreCase("null")) {
@@ -48,25 +50,20 @@ public AuthResponse(final RestResponse restResponse, final int retries) {
4850
}
4951
authClientToken = authJsonObject.getString("client_token", "");
5052
final JsonArray authPoliciesJsonArray = authJsonObject.get("policies").asArray();
51-
authPolicies = new ArrayList<String>();
53+
authPolicies = new ArrayList<>();
5254
for (final JsonValue authPolicy : authPoliciesJsonArray) {
5355
authPolicies.add(authPolicy.asString());
5456
}
5557
} catch (UnsupportedEncodingException | ParseException e) {
56-
e.printStackTrace();
5758
}
5859
}
5960

6061
public String getUsername() {
6162
return username;
6263
}
6364

64-
/**
65-
* @return Deprecated. Use <code>getRenewable()</code> (returning object <code>Boolean</code> rather than primitive <code>boolean</code>.
66-
*/
67-
@Deprecated
68-
public boolean isRenewable() {
69-
return getRenewable() == null ? false : getRenewable();
65+
public Boolean getRenewable() {
66+
return renewable;
7067
}
7168

7269
public String getAuthClientToken() {

src/main/java/com/bettercloud/vault/response/LogicalResponse.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.bettercloud.vault.response;
22

3+
import com.bettercloud.vault.json.Json;
4+
import com.bettercloud.vault.json.JsonObject;
35
import com.bettercloud.vault.rest.RestResponse;
46

57
import java.util.HashMap;
@@ -12,6 +14,9 @@
1214
public class LogicalResponse extends VaultResponse {
1315

1416
private Map<String, String> data = new HashMap<String, String>();
17+
private String leaseId;
18+
private Boolean renewable;
19+
private Long leaseDuration;
1520

1621
/**
1722
* This constructor simply exposes the common base class constructor.
@@ -21,6 +26,7 @@ public class LogicalResponse extends VaultResponse {
2126
*/
2227
public LogicalResponse(final RestResponse restResponse, final int retries) {
2328
super(restResponse, retries);
29+
parseMetadataFields();
2430
}
2531

2632
/**
@@ -37,14 +43,35 @@ public LogicalResponse(
3743
) {
3844
super(restResponse, retries);
3945
this.data.putAll(data);
46+
parseMetadataFields();
4047
}
4148

4249
public Map<String, String> getData() {
4350
return data;
4451
}
4552

46-
@Deprecated
47-
public void setData(final Map<String, String> data) {
48-
this.data = data;
53+
public String getLeaseId() {
54+
return leaseId;
55+
}
56+
57+
public Boolean getRenewable() {
58+
return renewable;
59+
}
60+
61+
public Long getLeaseDuration() {
62+
return leaseDuration;
63+
}
64+
65+
private void parseMetadataFields() {
66+
try {
67+
final String jsonString = new String(getRestResponse().getBody(), "UTF-8");
68+
final JsonObject jsonObject = Json.parse(jsonString).asObject();
69+
70+
this.leaseId = jsonObject.get("lease_id").asString();
71+
this.renewable = jsonObject.get("renewable").asBoolean();
72+
this.leaseDuration = jsonObject.get("lease_duration").asLong();
73+
} catch (Exception e) {
74+
return;
75+
}
4976
}
5077
}
Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.bettercloud.vault.response;
22

3-
import com.bettercloud.vault.json.Json;
43
import com.bettercloud.vault.rest.RestResponse;
54

65
/**
@@ -20,92 +19,20 @@ public class VaultResponse {
2019
private RestResponse restResponse;
2120
private int retries;
2221

23-
@Deprecated
24-
private String leaseId;
25-
@Deprecated
26-
private Boolean renewable;
27-
@Deprecated
28-
private Long leaseDuration;
29-
3022
/**
3123
* @param restResponse The raw HTTP response from Vault.
3224
* @param retries The number of retry attempts that occurred during the API call (can be zero).
3325
*/
3426
public VaultResponse(final RestResponse restResponse, final int retries) {
3527
this.restResponse = restResponse;
3628
this.retries = retries;
37-
parseMetadataFields();
3829
}
3930

4031
public RestResponse getRestResponse() {
4132
return restResponse;
4233
}
4334

44-
@Deprecated
45-
public void setRestResponse(final RestResponse restResponse) {
46-
this.restResponse = restResponse;
47-
parseMetadataFields();
48-
}
49-
5035
public int getRetries() {
5136
return retries;
5237
}
53-
54-
@Deprecated
55-
public void setRetries(final int retries) {
56-
this.retries = retries;
57-
}
58-
59-
public String getLeaseId() {
60-
return leaseId;
61-
}
62-
63-
@Deprecated
64-
public void setLeaseId(final String leaseId) {
65-
this.leaseId = leaseId;
66-
}
67-
68-
public Boolean getRenewable() {
69-
return renewable;
70-
}
71-
72-
@Deprecated
73-
public void setRenewable(final Boolean renewable) {
74-
this.renewable = renewable;
75-
}
76-
77-
@Deprecated
78-
public void baseSetRenewable(final Boolean renewable) {
79-
this.renewable = renewable;
80-
}
81-
82-
public Long getLeaseDuration() {
83-
return leaseDuration;
84-
}
85-
86-
@Deprecated
87-
public void setLeaseDuration(final Long leaseDuration) {
88-
this.leaseDuration = leaseDuration;
89-
}
90-
91-
private void parseMetadataFields() {
92-
String jsonString;
93-
try {
94-
jsonString = new String(this.restResponse.getBody(), "UTF-8");
95-
} catch (Exception e) {
96-
return;
97-
}
98-
try {
99-
this.leaseId = Json.parse(jsonString).asObject().get("lease_id").asString();
100-
} catch (Exception e) {
101-
}
102-
try {
103-
this.renewable = Json.parse(jsonString).asObject().get("renewable").asBoolean();
104-
} catch (Exception e) {
105-
}
106-
try {
107-
this.leaseDuration = Json.parse(jsonString).asObject().get("lease_duration").asLong();
108-
} catch (Exception e) {
109-
}
110-
}
11138
}

0 commit comments

Comments
 (0)