From 9f7902858948f73087c7784b0521f72921894403 Mon Sep 17 00:00:00 2001 From: Edo Demirbilek Date: Fri, 1 Sep 2023 10:28:21 -0400 Subject: [PATCH] Adding access key auth support for openstack V3 from an earlier commit + adding test --- .../keystone/v3/auth/V3AuthenticationApi.java | 10 +++++ .../BindAccessKeyAuthToJsonPayload.java | 44 +++++++++++++++++++ .../BindPasswordAuthToJsonPayload.java | 2 +- .../binders/BindTokenAuthToJsonPayload.java | 2 +- .../openstack/keystone/v3/domain/Auth.java | 20 +++++++-- .../v3/auth/V3AuthenticationApiLiveTest.java | 7 +++ .../v3/auth/V3AuthenticationApiMockTest.java | 10 +++++ .../src/test/resources/v3/auth-accesskey.json | 13 ++++++ 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindAccessKeyAuthToJsonPayload.java create mode 100644 apis/openstack-keystone/src/test/resources/v3/auth-accesskey.json diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApi.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApi.java index 0902f659a61..cac54f34ab2 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApi.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApi.java @@ -27,6 +27,9 @@ import org.jclouds.openstack.keystone.auth.domain.PasswordCredentials; import org.jclouds.openstack.keystone.auth.domain.TenantOrDomainAndCredentials; import org.jclouds.openstack.keystone.auth.domain.TokenCredentials; +import org.jclouds.openstack.keystone.auth.domain.ApiAccessKeyCredentials; +import org.jclouds.openstack.keystone.auth.domain.AuthInfo; +import org.jclouds.openstack.keystone.v3.binders.BindAccessKeyAuthToJsonPayload; import org.jclouds.openstack.keystone.v3.binders.BindPasswordAuthToJsonPayload; import org.jclouds.openstack.keystone.v3.binders.BindTokenAuthToJsonPayload; import org.jclouds.openstack.keystone.v3.domain.Token; @@ -57,4 +60,11 @@ public interface V3AuthenticationApi extends AuthenticationApi, Closeable { @Override Token authenticateToken(TenantOrDomainAndCredentials credentials); + @Named("token:create") + @POST + @ResponseParser(ParseTokenFromHttpResponse.class) + @MapBinder(BindAccessKeyAuthToJsonPayload.class) + @Override + AuthInfo authenticateAccessKey(TenantOrDomainAndCredentials credentials); + } diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindAccessKeyAuthToJsonPayload.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindAccessKeyAuthToJsonPayload.java new file mode 100644 index 00000000000..1bee2291642 --- /dev/null +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindAccessKeyAuthToJsonPayload.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.openstack.keystone.v3.binders; + +import org.jclouds.json.Json; +import org.jclouds.openstack.keystone.auth.domain.ApiAccessKeyCredentials; +import org.jclouds.openstack.keystone.auth.domain.TenantOrDomainAndCredentials; +import org.jclouds.openstack.keystone.v3.domain.Auth; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import static java.util.Collections.singletonList; + +@Singleton +public class BindAccessKeyAuthToJsonPayload extends BindAuthToJsonPayload { + + @Inject + protected BindAccessKeyAuthToJsonPayload(Json jsonBinder) { + super(jsonBinder); + } + + @Override + protected Auth buildAuth(TenantOrDomainAndCredentials credentials, Object scope) { + Auth.Identity.AccessKeyAuth accessKeyAuth = Auth.Identity.AccessKeyAuth.create( + credentials.credentials().accessKey(), + credentials.credentials().secretKey()); + return Auth.create(Auth.Identity.create(singletonList("application_credential"), null, null, accessKeyAuth), null); + } +} \ No newline at end of file diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindPasswordAuthToJsonPayload.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindPasswordAuthToJsonPayload.java index 40c94f89ec8..5e0ce7b22a0 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindPasswordAuthToJsonPayload.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindPasswordAuthToJsonPayload.java @@ -44,7 +44,7 @@ protected Auth buildAuth(TenantOrDomainAndCredentials crede DomainAuth domain = DomainAuth.create(credentials.tenantOrDomainName()); UserAuth user = UserAuth.create(creds.username(), domain, creds.password()); - return Auth.create(Identity.create(singletonList("password"), null, PasswordAuth.create(user)), scope); + return Auth.create(Identity.create(singletonList("password"), null, PasswordAuth.create(user), null), scope); } } diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindTokenAuthToJsonPayload.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindTokenAuthToJsonPayload.java index 439baa29ebc..a3e01a6d47a 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindTokenAuthToJsonPayload.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/binders/BindTokenAuthToJsonPayload.java @@ -39,7 +39,7 @@ public class BindTokenAuthToJsonPayload extends BindAuthToJsonPayload credentials, Object scope) { Id token = Id.create(credentials.credentials().id()); - return Auth.create(Identity.create(singletonList("token"), token, null), scope); + return Auth.create(Identity.create(singletonList("token"), token, null, null), scope); } } diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/domain/Auth.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/domain/Auth.java index fcfe4faaf94..25ab9bc6fc6 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/domain/Auth.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v3/domain/Auth.java @@ -45,9 +45,23 @@ public abstract static class Identity { @Nullable public abstract PasswordAuth password(); - @SerializedNames({ "methods", "token", "password" }) - public static Identity create(List methods, Id token, PasswordAuth password) { - return new AutoValue_Auth_Identity(methods, token, password); + @Nullable + public abstract AccessKeyAuth secret(); + + @SerializedNames({ "methods", "token", "password", "application_credential" }) + public static Identity create(List methods, Id token, PasswordAuth password, AccessKeyAuth accessKeyAuth) { + return new AutoValue_Auth_Identity(methods, token, password, accessKeyAuth); + } + + @AutoValue + public abstract static class AccessKeyAuth { + public abstract String id(); + public abstract String secret(); + + @SerializedNames({ "id", "secret" }) + public static AccessKeyAuth create(String id, String secret) { + return new AutoValue_Auth_Identity_AccessKeyAuth(id, secret); + } } @AutoValue diff --git a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiLiveTest.java b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiLiveTest.java index 3b8a3b48c35..aba908bdf15 100644 --- a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiLiveTest.java +++ b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiLiveTest.java @@ -23,6 +23,7 @@ import org.jclouds.openstack.keystone.auth.domain.PasswordCredentials; import org.jclouds.openstack.keystone.auth.domain.TenantOrDomainAndCredentials; import org.jclouds.openstack.keystone.auth.domain.TokenCredentials; +import org.jclouds.openstack.keystone.auth.domain.ApiAccessKeyCredentials; import org.jclouds.openstack.keystone.v3.internal.BaseV3KeystoneApiLiveTest; import org.testng.annotations.Test; @@ -54,4 +55,10 @@ public void testAuthenticateToken() { .tenantOrDomainName(tenant).scope("unscoped") .credentials(TokenCredentials.builder().id(token.get()).build()).build())); } + + public void testAuthenticateAccessKey() { + assertNotNull(authenticationApi.authenticateAccessKey(TenantOrDomainAndCredentials. builder() + .tenantOrDomainName(tenant).scope("unscoped") + .credentials(ApiAccessKeyCredentials.builder().accessKey(identity).secretKey(credential).build()).build())); + } } diff --git a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiMockTest.java b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiMockTest.java index fac5d52bde2..771a8b227cb 100644 --- a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiMockTest.java +++ b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v3/auth/V3AuthenticationApiMockTest.java @@ -160,4 +160,14 @@ private void checkTokenResult(TenantOrDomainAndCredentials credentials, Strin assertSent(server, "POST", "/auth/tokens", stringFromResource(json)); } + public void testAuthenticateAccessKey() throws InterruptedException { + + TenantOrDomainAndCredentials credentials = TenantOrDomainAndCredentials + . builder().tenantOrDomainName("domain").scope("unscoped") + .credentials(ApiAccessKeyCredentials.builder().accessKey("identity").secretKey("credential").build()).build(); + + + checkTokenResult(credentials, "/v3/auth-accesskey.json"); + } + } diff --git a/apis/openstack-keystone/src/test/resources/v3/auth-accesskey.json b/apis/openstack-keystone/src/test/resources/v3/auth-accesskey.json new file mode 100644 index 00000000000..acc185140d8 --- /dev/null +++ b/apis/openstack-keystone/src/test/resources/v3/auth-accesskey.json @@ -0,0 +1,13 @@ +{ + "auth": { + "identity": { + "methods": [ + "application_credential" + ], + "application_credential": { + "id": "identity", + "secret": "credential" + } + } + } +}