Adding access key auth support for openstack V3 (#183)

* Adding access key auth support for openstack V3 from an earlier commit + adding test

* added new line at the end to fix checkstyle warning
This commit is contained in:
Edo Demirbilek 2023-09-04 17:49:45 -04:00 committed by GitHub
parent 6b49389610
commit 611b4c4a11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 103 additions and 5 deletions

View File

@ -27,6 +27,9 @@ import org.jclouds.openstack.keystone.auth.AuthenticationApi;
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<TokenCredentials> credentials);
@Named("token:create")
@POST
@ResponseParser(ParseTokenFromHttpResponse.class)
@MapBinder(BindAccessKeyAuthToJsonPayload.class)
@Override
AuthInfo authenticateAccessKey(TenantOrDomainAndCredentials<ApiAccessKeyCredentials> credentials);
}

View File

@ -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<ApiAccessKeyCredentials> {
@Inject
protected BindAccessKeyAuthToJsonPayload(Json jsonBinder) {
super(jsonBinder);
}
@Override
protected Auth buildAuth(TenantOrDomainAndCredentials<ApiAccessKeyCredentials> 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);
}
}

View File

@ -44,7 +44,7 @@ public class BindPasswordAuthToJsonPayload extends BindAuthToJsonPayload<Passwor
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);
}
}

View File

@ -39,7 +39,7 @@ public class BindTokenAuthToJsonPayload extends BindAuthToJsonPayload<TokenCrede
@Override
protected Auth buildAuth(TenantOrDomainAndCredentials<TokenCredentials> 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);
}
}

View File

@ -45,9 +45,23 @@ public abstract class Auth {
@Nullable
public abstract PasswordAuth password();
@SerializedNames({ "methods", "token", "password" })
public static Identity create(List<String> 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<String> 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

View File

@ -23,6 +23,7 @@ import java.util.Properties;
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 class V3AuthenticationApiLiveTest extends BaseV3KeystoneApiLiveTest {
.tenantOrDomainName(tenant).scope("unscoped")
.credentials(TokenCredentials.builder().id(token.get()).build()).build()));
}
public void testAuthenticateAccessKey() {
assertNotNull(authenticationApi.authenticateAccessKey(TenantOrDomainAndCredentials.<ApiAccessKeyCredentials> builder()
.tenantOrDomainName(tenant).scope("unscoped")
.credentials(ApiAccessKeyCredentials.builder().accessKey(identity).secretKey(credential).build()).build()));
}
}

View File

@ -160,4 +160,14 @@ public class V3AuthenticationApiMockTest extends BaseV3KeystoneApiMockTest {
assertSent(server, "POST", "/auth/tokens", stringFromResource(json));
}
public void testAuthenticateAccessKey() throws InterruptedException {
TenantOrDomainAndCredentials<ApiAccessKeyCredentials> credentials = TenantOrDomainAndCredentials
.<ApiAccessKeyCredentials> builder().tenantOrDomainName("domain").scope("unscoped")
.credentials(ApiAccessKeyCredentials.builder().accessKey("identity").secretKey("credential").build()).build();
checkTokenResult(credentials, "/v3/auth-accesskey.json");
}
}

View File

@ -0,0 +1,13 @@
{
"auth": {
"identity": {
"methods": [
"application_credential"
],
"application_credential": {
"id": "identity",
"secret": "credential"
}
}
}
}