Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FOR 5771 identify] improve the code quality of nacos-client. #6005

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
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 @@ -43,7 +43,7 @@ public final class CredentialService implements SpasCredentialLoader {

private CredentialService(String appName) {
if (appName == null) {
String value = System.getProperty("project.name");
String value = System.getProperty(IdentifyConstants.PROJECT_NAME_PROPERTY);
if (StringUtils.isNotEmpty(value)) {
appName = value;
}
Expand All @@ -59,12 +59,13 @@ public static CredentialService getInstance() {
public static CredentialService getInstance(String appName) {
String key = appName != null ? appName : IdentifyConstants.NO_APP_NAME;
CredentialService instance = INSTANCES.get(key);
if (instance == null) {
instance = new CredentialService(appName);
CredentialService previous = INSTANCES.putIfAbsent(key, instance);
if (previous != null) {
instance = previous;
}
if (instance != null) {
return instance;
}
instance = new CredentialService(appName);
CredentialService previous = INSTANCES.putIfAbsent(key, instance);
if (previous != null) {
instance = previous;
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void stop() {
}

private void loadCredential(boolean init) {
boolean logWarn = init;
if (propertyPath == null) {
URL url = ClassLoader.getSystemResource(IdentifyConstants.PROPERTIES_FILENAME);
if (url != null) {
Expand All @@ -123,12 +122,12 @@ private void loadCredential(boolean init) {
IdentifyConstants.CREDENTIAL_PATH + (appName == null ? IdentifyConstants.CREDENTIAL_DEFAULT
: appName);
} else {
if (logWarn) {
if (init) {
SPAS_LOGGER.info("[{}] Defined credential file: -Dspas.identity={}", appName, propertyPath);
}
}
} else {
if (logWarn) {
if (init) {
SPAS_LOGGER.info("[{}] Load credential file from classpath: {}", appName,
IdentifyConstants.PROPERTIES_FILENAME);
}
Expand Down Expand Up @@ -161,7 +160,7 @@ private void loadCredential(boolean init) {
accessKey = System.getenv(IdentifyConstants.ENV_ACCESS_KEY);
secretKey = System.getenv(IdentifyConstants.ENV_SECRET_KEY);
if (accessKey == null && secretKey == null) {
if (logWarn) {
if (init) {
SPAS_LOGGER.info("{} No credential found", appName);
}
return;
Expand All @@ -184,7 +183,7 @@ private void loadCredential(boolean init) {
}
}

if (logWarn) {
if (init) {
SPAS_LOGGER.info("[{}] Load credential file {}", appName, propertyPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ public class IdentifyConstants {

public static final String NO_APP_NAME = "";

public static final String PROJECT_NAME_PROPERTY = "project.name";

public static final String RAM_ROLE_NAME_PROPERTY = "ram.role.name";

public static final String REFRESH_TIME_PROPERTY = "time.to.refresh.in.millisecond";

public static final String SECURITY_PROPERTY = "security.credentials";

public static final String SECURITY_URL_PROPERTY = "security.credentials.url";

public static final String SECURITY_CACHE_PROPERTY = "cache.security.credentials";
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ public class StsConfig {
private String ramRoleName;

/**
* STS 临时凭证有效期剩余多少时开始刷新(允许本地时间比 STS 服务时间最多慢多久).
* The STS temporary certificate will be refreshed when the validity period of
* the temporary certificate is left (allow the local time to be at most slower than the STS service time).
*/
private int timeToRefreshInMillisecond = 3 * 60 * 1000;

/**
* 获取 STS 临时凭证的元数据接口(包含角色名称).
* Metadata interface for obtaining STS temporary credentials (including role name).
*/
private String securityCredentialsUrl;

/**
* 设定 STS 临时凭证,不再通过元数据接口获取.
* Set the STS temporary certificate and no longer obtain it through the metadata interface.
*/
private String securityCredentials;

/**
* 是否缓存.
* Whether to cache.
*/
private boolean cacheSecurityCredentials = true;

Expand All @@ -55,27 +56,27 @@ private static class Singleton {
}

private StsConfig() {
String ramRoleName = System.getProperty("ram.role.name");
String ramRoleName = System.getProperty(IdentifyConstants.RAM_ROLE_NAME_PROPERTY);
if (!StringUtils.isBlank(ramRoleName)) {
setRamRoleName(ramRoleName);
}

String timeToRefreshInMillisecond = System.getProperty("time.to.refresh.in.millisecond");
String timeToRefreshInMillisecond = System.getProperty(IdentifyConstants.REFRESH_TIME_PROPERTY);
if (!StringUtils.isBlank(timeToRefreshInMillisecond)) {
setTimeToRefreshInMillisecond(Integer.parseInt(timeToRefreshInMillisecond));
}

String securityCredentials = System.getProperty("security.credentials");
String securityCredentials = System.getProperty(IdentifyConstants.SECURITY_PROPERTY);
if (!StringUtils.isBlank(securityCredentials)) {
setSecurityCredentials(securityCredentials);
}

String securityCredentialsUrl = System.getProperty("security.credentials.url");
String securityCredentialsUrl = System.getProperty(IdentifyConstants.SECURITY_URL_PROPERTY);
if (!StringUtils.isBlank(securityCredentialsUrl)) {
setSecurityCredentialsUrl(securityCredentialsUrl);
}

String cacheSecurityCredentials = System.getProperty("cache.security.credentials");
String cacheSecurityCredentials = System.getProperty(IdentifyConstants.SECURITY_CACHE_PROPERTY);
if (!StringUtils.isBlank(cacheSecurityCredentials)) {
setCacheSecurityCredentials(Boolean.parseBoolean(cacheSecurityCredentials));
}
Expand Down