【问题标题】:Error with Spring AppRoleAuthentication - URI is not absoluteSpring AppRoleAuthentication 出错 - URI 不是绝对的
【发布时间】:2021-07-09 18:51:05
【问题描述】:

我正在尝试使用 AppRole 身份验证从保管库中检索机密。但我得到了错误:

java.lang.IllegalArgumentException: URI 不是绝对的

我要做的是创建一个 vaultEndpoint,然后根据选择的方法使用令牌身份验证或 AppRole 身份验证。令牌身份验证没有问题,但是每当我尝试检索密钥甚至让 vaultToken 使用 AppRole 登录时,都会发生 URI is not absolute 错误。

我在https://docs.oracle.com/javase/8/docs/api/java/net/URI.html 中看到,当URI 指定方案时它是绝对的,否则它是相对的。但我认为我的 URI 指定了一个方案。

所以我在这里有点迷路了。 有谁知道我做错了什么?或者为什么我会收到这个错误?

我使用 spring-vault-core-2.2.0.RELEASE

这是我的代码:

VaultEndpoint ep = VaultEndpoint.create(host, portInt);
    if (scheme != null) {
        ep.setScheme(scheme);
    }

    if (authMethod.equals("token")) {
        vaultTemplate = new VaultTemplate(ep, new TokenAuthentication(token));

    } else if (authMethod.equals("appRole")) {

        RestOperations restOperations = VaultClients.createRestTemplate();

        AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder()
                .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId))
                .secretId(AppRoleAuthenticationOptions.SecretId.wrapped(VaultToken.of(secretId))).build();

        vaultTemplate = new VaultTemplate(ep, new AppRoleAuthentication(options, restOperations));
    }
  }

如果我尝试获取 vaultToken,我也会遇到同样的错误:

            RestOperations restOperations = VaultClients.createRestTemplate();
        AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder()
                .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId))
                .secretId(AppRoleAuthenticationOptions.SecretId.wrapped(VaultToken.of(uncryptedSecretId))).build();

        AppRoleAuthentication appRoleAuth = new AppRoleAuthentication(options, restOperations);
        VaultToken appRoleToken = appRoleAuth.login();

这是错误:

java.lang.IllegalArgumentException: URI is not absolute
    at java.net.URI.toURL(Unknown Source)
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145)
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:98)
    at org.springframework.vault.client.VaultClients.lambda$createRestTemplate$0(VaultClients.java:128)
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93)
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:77)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:742)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:586)
    at org.springframework.vault.authentication.AppRoleAuthentication.getSecretId(AppRoleAuthentication.java:305)
    at org.springframework.vault.authentication.AppRoleAuthentication.getAppRoleLoginBody(AppRoleAuthentication.java:344)
    at org.springframework.vault.authentication.AppRoleAuthentication.createTokenUsingAppRole(AppRoleAuthentication.java:201)
    at org.springframework.vault.authentication.AppRoleAuthentication.login(AppRoleAuthentication.java:191)

更新

经过进一步调查,问题是我如何实例化 restTemplate。 我将 spring 上下文库添加到我的项目中并实现了 AbstractVaultConfiguration 类。此类包含解决我的问题的 restOperations() 函数。

这就是我解决问题的方法:

public class AppRoleAuthenticationService extends AbstractVaultConfiguration {

private String roleId;
private String secretId;
private String host;
private String scheme;
private String port;

public AppRoleAuthenticationService(String roleId, String secretId, String host, String scheme, String port) {
    this.roleId = roleId;
    this.secretId = secretId;
    this.host = host;
    this.scheme = scheme;
    this.port = port;
}

@Override
public VaultEndpoint vaultEndpoint() {
    int portInt = Integer.parseInt(port);
    VaultEndpoint ep = VaultEndpoint.create(host, portInt);
    if (scheme != null) {
        ep.setScheme(scheme);
    }

    return ep;
}

@Override
public ClientAuthentication clientAuthentication() {

    AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder()
            .roleId(AppRoleAuthenticationOptions.RoleId.provided(roleId))
            .secretId(AppRoleAuthenticationOptions.SecretId.provided(secretId)).build();

    return new AppRoleAuthentication(options, restOperations());
}

}

然后就使用这个类:

AppRoleAuthenticationService appRoleAuth = new AppRoleAuthenticationService(roleId, 
      uncryptedSecretId, host, scheme, port);
VaultEndpoint vaultEp = appRoleAuth.vaultEndpoint();
ClientAuthentication auth = appRoleAuth.clientAuthentication();

vaultTemplate = new VaultTemplate(vaultEp, auth);

【问题讨论】:

    标签: java spring hashicorp-vault spring-vault


    【解决方案1】:

    我设法解决了我的问题,我更新了原始帖子以分享答案。

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2014-07-02
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多