Skip to content

Commit

Permalink
fix: Ecwid#186 token as request param is deprecated
Browse files Browse the repository at this point in the history
- more places where token was still used as url parameter
- throw Hard Runtime error is we still use token as url parameter
  • Loading branch information
lucwillems committed Mar 27, 2024
1 parent dbcda19 commit a95fb09
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 96 deletions.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.1.0</version>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ecwid/consul/SingleUrlParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public List<String> toUrlParameters() {
}
}

public boolean isToken() {
return key.equalsIgnoreCase("token");
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ecwid/consul/UrlParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
public interface UrlParameters {

public List<String> toUrlParameters();

}
8 changes: 7 additions & 1 deletion src/main/java/com/ecwid/consul/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public static String generateUrl(String baseUrl, List<UrlParameters> params) {
List<String> allParams = new ArrayList<String>();
for (UrlParameters item : params) {
if (item != null) {
//Guard for url based tokens
if (item instanceof SingleUrlParameters && ((SingleUrlParameters) item).isToken()) {
throw new RuntimeException("token url parameter found for: " + baseUrl);
}
allParams.addAll(item.toUrlParameters());
}
}
Expand All @@ -60,7 +64,9 @@ public static String generateUrl(String baseUrl, List<UrlParameters> params) {

public static Map<String, String> createTokenMap(String token) {
Map<String, String> headers = new HashMap<>();
headers.put("X-Consul-Token", token);
if (token!=null) {
headers.put("X-Consul-Token", token);
}
return headers;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ecwid/consul/v1/ConsulClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ public Response<Session> getSessionInfo(String session, QueryParams queryParams)

@Override
public Response<Session> getSessionInfo(String session, QueryParams queryParams, String token) {
return sessionClient.getSessionInfo(session, queryParams, token);
return sessionClient.getSessionInfo(session, queryParams);
}

@Override
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/com/ecwid/consul/v1/ConsulRawClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,21 @@ public ConsulRawClient(HttpClient httpClient, String host, int port, String path
}

public HttpResponse makeGetRequest(String endpoint, UrlParameters... urlParams) {
return makeGetRequest(endpoint, Arrays.asList(urlParams));
return makeGetRequest(endpoint, null,Arrays.asList(urlParams));
}

public HttpResponse makeGetRequest(String endpoint, List<UrlParameters> urlParams) {
public HttpResponse makeGetRequest(String endpoint, String token,UrlParameters... urlParams) {
return makeGetRequest(endpoint,token, Arrays.asList(urlParams));
}
public HttpResponse makeGetRequest(String endpoint,List<UrlParameters> urlParams) {
return makeGetRequest(endpoint,null,urlParams);
}
public HttpResponse makeGetRequest(String endpoint, String token,List<UrlParameters> urlParams) {
String url = prepareUrl(agentAddress + endpoint);
url = Utils.generateUrl(url, urlParams);

HttpRequest request = HttpRequest.Builder.newBuilder()
.setUrl(url)
.addHeaders(Utils.createTokenMap(token))
.build();

return httpTransport.makeGetRequest(request);
Expand All @@ -152,12 +158,18 @@ public HttpResponse makeGetRequest(Request request) {
}

public HttpResponse makePutRequest(String endpoint, String content, UrlParameters... urlParams) {
return this.makePutRequest(endpoint,content,null,urlParams);
}

@Deprecated
public HttpResponse makePutRequest(String endpoint, String content, String token, UrlParameters... urlParams) {
String url = prepareUrl(agentAddress + endpoint);
url = Utils.generateUrl(url, urlParams);

HttpRequest request = HttpRequest.Builder.newBuilder()
.setUrl(url)
.setContent(content)
.addHeaders(Utils.createTokenMap(token))
.build();

return httpTransport.makePutRequest(request);
Expand All @@ -167,14 +179,12 @@ public HttpResponse makePutRequest(Request request) {
//, String endpoint, byte[] binaryContent, UrlParameters... urlParams
String url = prepareUrl(agentAddress + request.getEndpoint());
url = Utils.generateUrl(url, request.getUrlParameters());

HttpRequest httpRequest = HttpRequest.Builder.newBuilder()
.setUrl(url)
.setContent(request.getContent()) // if content is not null then it has priority over binaryContent
.setBinaryContent(request.getBinaryContent())
.addHeaders(Utils.createTokenMap(request.getToken()))
.build();

HttpRequest.Builder builder = HttpRequest.Builder.newBuilder();
builder.setUrl(url)
.setContent(request.getContent()) // if content is not null then it has priority over binaryContent
.setBinaryContent(request.getBinaryContent())
.addHeaders(Utils.createTokenMap(request.getToken()));
HttpRequest httpRequest = builder.build();
return httpTransport.makePutRequest(httpRequest);
}

Expand Down
23 changes: 8 additions & 15 deletions src/main/java/com/ecwid/consul/v1/acl/AclConsulClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.ecwid.consul.v1.acl;

import java.util.List;
import java.util.Map;

import com.ecwid.consul.ConsulException;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
Expand All @@ -17,6 +12,9 @@
import com.ecwid.consul.v1.acl.model.UpdateAcl;
import com.google.gson.reflect.TypeToken;

import java.util.List;
import java.util.Map;

/**
* @author Vasily Vasilkov ([email protected])
*/
Expand Down Expand Up @@ -54,9 +52,8 @@ public AclConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) {

@Override
public Response<String> aclCreate(NewAcl newAcl, String token) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
String json = GsonFactory.getGson().toJson(newAcl);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/create", json, tokenParams);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/create", json,token);

if (httpResponse.getStatusCode() == 200) {
Map<String, String> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<Map<String, String>>() {
Expand All @@ -69,9 +66,8 @@ public Response<String> aclCreate(NewAcl newAcl, String token) {

@Override
public Response<Void> aclUpdate(UpdateAcl updateAcl, String token) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
String json = GsonFactory.getGson().toJson(updateAcl);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/update", json, tokenParams);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/update", json, token);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -82,8 +78,7 @@ public Response<Void> aclUpdate(UpdateAcl updateAcl, String token) {

@Override
public Response<Void> aclDestroy(String aclId, String token) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/destroy/" + aclId, "", tokenParams);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/destroy/" + aclId, "", token);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand Down Expand Up @@ -114,8 +109,7 @@ public Response<Acl> getAcl(String id) {

@Override
public Response<String> aclClone(String aclId, String token) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/clone/" + aclId, "", tokenParams);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/clone/" + aclId, "", token);

if (httpResponse.getStatusCode() == 200) {
Map<String, String> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<Map<String, String>>() {
Expand All @@ -128,8 +122,7 @@ public Response<String> aclClone(String aclId, String token) {

@Override
public Response<List<Acl>> getAclList(String token) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/acl/list", tokenParams);
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/acl/list", token);

if (httpResponse.getStatusCode() == 200) {
List<Acl> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Acl>>() {
Expand Down
34 changes: 10 additions & 24 deletions src/main/java/com/ecwid/consul/v1/agent/AgentConsulClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public Response<Self> getAgentSelf() {

@Override
public Response<Self> getAgentSelf(String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/self", tokenParam);
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/self", token);

if (httpResponse.getStatusCode() == 200) {
Self self = GsonFactory.getGson().fromJson(httpResponse.getContent(), Self.class);
Expand All @@ -116,9 +114,9 @@ public Response<Void> agentSetMaintenance(boolean maintenanceEnabled) {
@Override
public Response<Void> agentSetMaintenance(boolean maintenanceEnabled, String reason) {
UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
UrlParameters reasonParamenter = reason != null ? new SingleUrlParameters("reason", reason) : null;
UrlParameters reasonParameter = reason != null ? new SingleUrlParameters("reason", reason) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/maintenance", "", maintenanceParameter, reasonParamenter);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/maintenance", "", maintenanceParameter, reasonParameter);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand Down Expand Up @@ -158,10 +156,8 @@ public Response<Void> agentCheckRegister(NewCheck newCheck) {

@Override
public Response<Void> agentCheckRegister(NewCheck newCheck, String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

String json = GsonFactory.getGson().toJson(newCheck);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/register", json, tokenParam);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/register", json, token);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -177,9 +173,7 @@ public Response<Void> agentCheckDeregister(String checkId) {

@Override
public Response<Void> agentCheckDeregister(String checkId, String token) {
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/deregister/" + checkId, "", tokenParameter);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/deregister/" + checkId, "", token);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -201,9 +195,7 @@ public Response<Void> agentCheckPass(String checkId, String note) {
@Override
public Response<Void> agentCheckPass(String checkId, String note, String token) {
UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/pass/" + checkId, "", noteParameter, tokenParameter);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/pass/" + checkId, "", token , noteParameter);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -225,9 +217,7 @@ public Response<Void> agentCheckWarn(String checkId, String note) {
@Override
public Response<Void> agentCheckWarn(String checkId, String note, String token) {
UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/warn/" + checkId, "", noteParameter, tokenParameter);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/warn/" + checkId, "",token,noteParameter);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -249,9 +239,8 @@ public Response<Void> agentCheckFail(String checkId, String note) {
@Override
public Response<Void> agentCheckFail(String checkId, String note, String token) {
UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/fail/" + checkId, "", noteParameter, tokenParameter);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/fail/" + checkId, "", token, noteParameter);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -267,10 +256,9 @@ public Response<Void> agentServiceRegister(NewService newService) {

@Override
public Response<Void> agentServiceRegister(NewService newService, String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

String json = GsonFactory.getGson().toJson(newService);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/register", json, tokenParam);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/register", json,token);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand All @@ -286,9 +274,7 @@ public Response<Void> agentServiceDeregister(String serviceId) {

@Override
public Response<Void> agentServiceDeregister(String serviceId, String token) {
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/deregister/" + serviceId, "", tokenParam);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/service/deregister/" + serviceId, "", token);

if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ecwid.consul.v1.catalog;

import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
Expand Down Expand Up @@ -55,9 +53,7 @@ public Response<Void> catalogRegister(CatalogRegistration catalogRegistration) {
@Override
public Response<Void> catalogRegister(CatalogRegistration catalogRegistration, String token) {
String json = GsonFactory.getGson().toJson(catalogRegistration);
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/catalog/register", json, tokenParam);
HttpResponse httpResponse = rawClient.makePutRequest("/v1/catalog/register", json, token);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
Expand All @@ -73,9 +69,12 @@ public Response<Void> catalogDeregister(CatalogDeregistration catalogDeregistrat
@Override
public Response<Void> catalogDeregister(CatalogDeregistration catalogDeregistration, String token) {
String json = GsonFactory.getGson().toJson(catalogDeregistration);
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

HttpResponse httpResponse = rawClient.makePutRequest("/v1/catalog/deregister", json, tokenParam);
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/catalog/deregister")
.setToken(token)
.setContent(json)
.build();
HttpResponse httpResponse = rawClient.makePutRequest(request);
if (httpResponse.getStatusCode() == 200) {
return new Response<Void>(null, httpResponse);
} else {
Expand All @@ -85,6 +84,7 @@ public Response<Void> catalogDeregister(CatalogDeregistration catalogDeregistrat

@Override
public Response<List<String>> getCatalogDatacenters() {

HttpResponse httpResponse = rawClient.makeGetRequest("/v1/catalog/datacenters");

if (httpResponse.getStatusCode() == 200) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/ecwid/consul/v1/event/EventListRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ public List<UrlParameters> asUrlParameters() {
params.add(queryParams);
}

if (token != null) {
params.add(new SingleUrlParameters("token", token));
}

return params;
}

Expand Down
Loading

0 comments on commit a95fb09

Please sign in to comment.