Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.auth.impl.process.HttpLoginProcessor;
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;

import io.sermant.core.utils.MapUtils;
import io.sermant.core.utils.StringUtils;
import io.sermant.implement.service.dynamicconfig.ConfigClient;
import io.sermant.implement.service.dynamicconfig.common.DynamicConstants;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -85,6 +87,8 @@

private static final String URL_WITHOUT_CONTENT = "/nacos/v1/cs/history/configs?tenant=";

private static final String LOGIN_URL = "/nacos/v1/auth/users/login";

private final Properties properties;

private final ConfigService configService;
Expand Down Expand Up @@ -352,12 +356,23 @@
private String getToken() {
if ((System.currentTimeMillis() - lastRefreshTime) >= TimeUnit.SECONDS
.toMillis(tokenTtl - TOKEN_REFRESH_WINDOW)) {
lastRefreshTime = System.currentTimeMillis();
HttpLoginProcessor httpLoginProcessor = new HttpLoginProcessor(
NamingHttpClientManager.getInstance().getNacosRestTemplate());
LoginIdentityContext loginIdentityContext = httpLoginProcessor.getResponse(properties);
lastToken = loginIdentityContext.getParameter(KEY_ACCESS_TOKEN);
tokenTtl = Long.parseLong(loginIdentityContext.getParameter(KEY_TOKEN_TTL));
StringBuilder requestUrl = new StringBuilder().append(HTTP_PROTOCOL).append(properties.getProperty(PropertyKeyConst.SERVER_ADDR))

Check failure on line 359 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Line is longer than 120 characters (found 141). Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:359:0: error: Line is longer than 120 characters (found 141). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.append(LOGIN_URL);
Map<String, String> formParams = new HashMap<>();
formParams.put(PropertyKeyConst.USERNAME, properties.getProperty(PropertyKeyConst.USERNAME));
formParams.put(PropertyKeyConst.PASSWORD, properties.getProperty(PropertyKeyConst.PASSWORD));
try {
lastRefreshTime = System.currentTimeMillis();
String result = doPost(requestUrl.toString(), formParams, false);
if (StringUtils.isBlank(result)) {
throw new RuntimeException("Nacos http request getToken exception, response is blank.");
}
JSONObject jsonObject = JSONObject.parseObject(result);
lastToken = jsonObject.getString(KEY_ACCESS_TOKEN);
tokenTtl = jsonObject.getLong(KEY_TOKEN_TTL);
} catch (IOException e) {
throw new RuntimeException("Nacos http request getToken exception.", e);
}
}
return lastToken;
}
Expand Down Expand Up @@ -390,4 +405,43 @@
}
}
}

/**
* HTTP post request
*
* @param url HTTP request url
* @param formParams form params
* @param returnStatusWhenHasError return status when has error
* @return response body
* @throws IOException IO exception during service invocation process
*/
private String doPost(String url, Map<String, String> formParams, boolean returnStatusWhenHasError)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In What Scenarios Is a StatusCode Return Required When an Invoking Failure Occurs?

throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
int timeOut = Integer.parseInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT));
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(timeOut) // Timeout for connecting to the host
.setConnectionRequestTimeout(timeOut) // Request timeout
.setSocketTimeout(timeOut) // Read timeout
.build();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
if (!MapUtils.isEmpty(formParams)) {
List<NameValuePair> parameters = new ArrayList<>();
for (Map.Entry<String, String> entry : formParams.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
}
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
}
LOGGER.error("Http post request for getting all nacos keys error, the message is: {}",
null!=response.getEntity()?EntityUtils.toString(response.getEntity()):"entity is null");

Check failure on line 442 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 ':' is not preceded with whitespace. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:442:94: error: ':' is not preceded with whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)

Check failure on line 442 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 ':' is not followed by whitespace. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:442:94: error: ':' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)

Check failure on line 442 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 '?' is not preceded with whitespace. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:442:51: error: '?' is not preceded with whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)

Check failure on line 442 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 '?' is not followed by whitespace. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:442:51: error: '?' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)

Check failure on line 442 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 '!=' is not preceded with whitespace. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:442:29: error: '!=' is not preceded with whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)

Check failure on line 442 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 '!=' is not followed by whitespace. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java:442:29: error: '!=' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)
return returnStatusWhenHasError ? String.valueOf(statusCode) : "";
}
}
}
}
Loading