Search in sources :

Example 1 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class AccessKeyAuthenticator method authenticate.

@Override
public void authenticate(Invocation invocation, URL url) throws RpcAuthenticationException {
    String accessKeyId = String.valueOf(invocation.getAttachment(Constants.AK_KEY));
    String requestTimestamp = String.valueOf(invocation.getAttachment(Constants.REQUEST_TIMESTAMP_KEY));
    String originSignature = String.valueOf(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY));
    String consumer = String.valueOf(invocation.getAttachment(CommonConstants.CONSUMER));
    if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(consumer) || StringUtils.isEmpty(requestTimestamp) || StringUtils.isEmpty(originSignature)) {
        throw new RpcAuthenticationException("Failed to authenticate, maybe consumer not enable the auth");
    }
    AccessKeyPair accessKeyPair = null;
    try {
        accessKeyPair = getAccessKeyPair(invocation, url);
    } catch (Exception e) {
        throw new RpcAuthenticationException("Failed to authenticate , can't load the accessKeyPair", e);
    }
    String computeSignature = getSignature(url, invocation, accessKeyPair.getSecretKey(), requestTimestamp);
    boolean success = computeSignature.equals(originSignature);
    if (!success) {
        throw new RpcAuthenticationException("Failed to authenticate, signature is not correct");
    }
}
Also used : AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair) RpcAuthenticationException(org.apache.dubbo.auth.exception.RpcAuthenticationException) RpcAuthenticationException(org.apache.dubbo.auth.exception.RpcAuthenticationException) AccessKeyNotFoundException(org.apache.dubbo.auth.exception.AccessKeyNotFoundException)

Example 2 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class AccessKeyAuthenticator method getAccessKeyPair.

AccessKeyPair getAccessKeyPair(Invocation invocation, URL url) {
    AccessKeyStorage accessKeyStorage = ExtensionLoader.getExtensionLoader(AccessKeyStorage.class).getExtension(url.getParameter(Constants.ACCESS_KEY_STORAGE_KEY, Constants.DEFAULT_ACCESS_KEY_STORAGE));
    AccessKeyPair accessKeyPair = null;
    try {
        accessKeyPair = accessKeyStorage.getAccessKey(url, invocation);
        if (accessKeyPair == null || StringUtils.isEmpty(accessKeyPair.getAccessKey()) || StringUtils.isEmpty(accessKeyPair.getSecretKey())) {
            throw new AccessKeyNotFoundException("AccessKeyId or secretAccessKey not found");
        }
    } catch (Exception e) {
        throw new RuntimeException("Can't load the AccessKeyPair from accessKeyStorage", e);
    }
    return accessKeyPair;
}
Also used : AccessKeyNotFoundException(org.apache.dubbo.auth.exception.AccessKeyNotFoundException) AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair) AccessKeyStorage(org.apache.dubbo.auth.spi.AccessKeyStorage) RpcAuthenticationException(org.apache.dubbo.auth.exception.RpcAuthenticationException) AccessKeyNotFoundException(org.apache.dubbo.auth.exception.AccessKeyNotFoundException)

Example 3 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class AccessKeyAuthenticator method sign.

@Override
public void sign(Invocation invocation, URL url) {
    String currentTime = String.valueOf(System.currentTimeMillis());
    String consumer = url.getParameter(CommonConstants.APPLICATION_KEY);
    AccessKeyPair accessKeyPair = getAccessKeyPair(invocation, url);
    invocation.setAttachment(Constants.REQUEST_SIGNATURE_KEY, getSignature(url, invocation, accessKeyPair.getSecretKey(), currentTime));
    invocation.setAttachment(Constants.REQUEST_TIMESTAMP_KEY, currentTime);
    invocation.setAttachment(Constants.AK_KEY, accessKeyPair.getAccessKey());
    invocation.setAttachment(CommonConstants.CONSUMER, consumer);
}
Also used : AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair)

Example 4 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class DefaultAccessKeyStorage method getAccessKey.

@Override
public AccessKeyPair getAccessKey(URL url, Invocation invocation) {
    AccessKeyPair accessKeyPair = new AccessKeyPair();
    String accessKeyId = url.getParameter(Constants.ACCESS_KEY_ID_KEY, Constants.DEFAULT_ACCESS_KEY_ID);
    String secretAccessKey = url.getParameter(Constants.SECRET_ACCESS_KEY_KEY);
    accessKeyPair.setAccessKey(accessKeyId);
    accessKeyPair.setSecretKey(secretAccessKey);
    return accessKeyPair;
}
Also used : AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair)

Example 5 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class DefaultAccessKeyStorageTest method testGetAccessKey.

@Test
void testGetAccessKey() {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk");
    DefaultAccessKeyStorage defaultAccessKeyStorage = new DefaultAccessKeyStorage();
    AccessKeyPair accessKey = defaultAccessKeyStorage.getAccessKey(url, mock(Invocation.class));
    assertNotNull(accessKey);
    assertEquals(accessKey.getAccessKey(), "ak");
    assertEquals(accessKey.getSecretKey(), "sk");
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

AccessKeyPair (org.apache.dubbo.auth.model.AccessKeyPair)7 URL (org.apache.dubbo.common.URL)3 Invocation (org.apache.dubbo.rpc.Invocation)3 Test (org.junit.jupiter.api.Test)3 AccessKeyNotFoundException (org.apache.dubbo.auth.exception.AccessKeyNotFoundException)2 RpcAuthenticationException (org.apache.dubbo.auth.exception.RpcAuthenticationException)2 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)2 AccessKeyStorage (org.apache.dubbo.auth.spi.AccessKeyStorage)1