Skip to content

Commit 94170ae

Browse files
committed
Merge pull request #438 from treasure-data/master
Add support for RAX API key authentication extension
2 parents 54f3e14 + b461220 commit 94170ae

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-2
lines changed

core/src/main/java/org/openstack4j/api/client/IOSClientBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public interface V2 extends IOSClientBuilder<OSClient, V2> {
9595
*/
9696
V2 tenantId(String tenantId);
9797

98+
/**
99+
* Use Rackspace API Key Authentication Admin Extension to authenticate
100+
*
101+
* @param raxApiKey true if enabled
102+
* @return self for method chaining
103+
*/
104+
V2 raxApiKey(boolean raxApiKey);
105+
98106
/**
99107
* Allows authentication via an existing Token versus {@link #credentials(String, String)}. Note: The
100108
* {@link #tenantId(String)} or {@link #tenantName(String)} will required when using this method for

core/src/main/java/org/openstack4j/openstack/client/OSClientBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.openstack4j.core.transport.Config;
1010
import org.openstack4j.model.common.Identifier;
1111
import org.openstack4j.openstack.identity.domain.Credentials;
12+
import org.openstack4j.openstack.identity.domain.RaxApiKeyCredentials;
1213
import org.openstack4j.openstack.identity.domain.TokenAuth;
1314
import org.openstack4j.openstack.identity.domain.v3.KeystoneAuth;
1415
import org.openstack4j.openstack.identity.domain.v3.KeystoneAuth.AuthScope;
@@ -72,6 +73,7 @@ public static class ClientV2 extends OSClientBuilder<OSClient, IOSClientBuilder.
7273
String tenantName;
7374
String tenantId;
7475
String tokenId;
76+
boolean raxApiKey;
7577

7678
@Override
7779
public ClientV2 tenantName(String tenantName) {
@@ -85,13 +87,23 @@ public ClientV2 tenantId(String tenantId) {
8587
return this;
8688
}
8789

90+
@Override
91+
public ClientV2 raxApiKey(boolean raxApiKey) {
92+
this.raxApiKey = raxApiKey;
93+
return this;
94+
}
95+
8896
@Override
8997
public OSClient authenticate() throws AuthenticationException {
9098
if (tokenId != null) {
9199
checkArgument(tenantName != null || tenantId != null, "TenantId or TenantName is required when using Token Auth");
92100
return OSAuthenticator.invoke(new TokenAuth(tokenId, tenantName, tenantId), endpoint, perspective, config);
93101
}
94102

103+
if (raxApiKey) {
104+
return OSAuthenticator.invoke(new RaxApiKeyCredentials(user, password), endpoint, perspective, config);
105+
}
106+
95107
return OSAuthenticator.invoke(new Credentials(user, password, tenantName, tenantId), endpoint, perspective, config);
96108
}
97109

core/src/main/java/org/openstack4j/openstack/identity/domain/Auth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public abstract class Auth implements ModelEntity {
88

99
private static final long serialVersionUID = 1L;
10-
public enum Type { CREDENTIALS, TOKEN };
10+
public enum Type { CREDENTIALS, TOKEN, RAX_APIKEY };
1111

1212
private String tenantId;
1313
private String tenantName;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.openstack4j.openstack.identity.domain;
2+
3+
import org.openstack4j.model.identity.AuthStore;
4+
import org.openstack4j.model.identity.AuthVersion;
5+
6+
import com.fasterxml.jackson.annotation.JsonIgnore;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.annotation.JsonRootName;
9+
10+
@JsonRootName("auth")
11+
public class RaxApiKeyCredentials extends Auth implements AuthStore {
12+
13+
private static final long serialVersionUID = 1L;
14+
15+
@JsonProperty("RAX-KSKEY:apiKeyCredentials")
16+
private ApiKeyCredentials apikeyCreds = new ApiKeyCredentials();
17+
18+
19+
public RaxApiKeyCredentials() {
20+
super(Type.RAX_APIKEY);
21+
}
22+
23+
public RaxApiKeyCredentials(String username, String apiKey) {
24+
this();
25+
apikeyCreds.setCredentials(username, apiKey);
26+
}
27+
28+
@Override
29+
@JsonIgnore
30+
public String getTenantId() {
31+
return super.getTenantId();
32+
}
33+
34+
@Override
35+
@JsonIgnore
36+
public String getTenantName() {
37+
return super.getTenantName();
38+
}
39+
40+
@JsonIgnore
41+
public String getUsername() {
42+
return apikeyCreds.username;
43+
}
44+
45+
@JsonIgnore
46+
@Override
47+
public String getPassword() {
48+
return getApiKey();
49+
}
50+
51+
@JsonIgnore
52+
public String getApiKey() {
53+
return apikeyCreds.apiKey;
54+
}
55+
56+
@SuppressWarnings("unchecked")
57+
@JsonIgnore
58+
@Override
59+
public <T> T unwrap() {
60+
return (T) this;
61+
}
62+
63+
@JsonIgnore
64+
@Override
65+
public String getId() {
66+
return getTenantId();
67+
}
68+
69+
@JsonIgnore
70+
@Override
71+
public String getName() {
72+
return getTenantName();
73+
}
74+
75+
private static final class ApiKeyCredentials {
76+
77+
@JsonProperty
78+
String username;
79+
@JsonProperty
80+
String apiKey;
81+
82+
public void setCredentials(String username, String apiKey) {
83+
this.username = username;
84+
this.apiKey = apiKey;
85+
}
86+
}
87+
88+
89+
@JsonIgnore
90+
@Override
91+
public AuthVersion getVersion() {
92+
return AuthVersion.V2;
93+
}
94+
}

core/src/main/java/org/openstack4j/openstack/internal/OSAuthenticator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.openstack4j.openstack.identity.domain.Auth;
1717
import org.openstack4j.openstack.identity.domain.Auth.Type;
1818
import org.openstack4j.openstack.identity.domain.Credentials;
19+
import org.openstack4j.openstack.identity.domain.RaxApiKeyCredentials;
1920
import org.openstack4j.openstack.identity.domain.KeystoneAccess;
2021
import org.openstack4j.openstack.identity.domain.TokenAuth;
2122
import org.openstack4j.openstack.identity.domain.v3.AccessWrapper;
@@ -44,7 +45,7 @@ public class OSAuthenticator {
4445
*/
4546
public static OSClient invoke(AuthStore auth, String endpoint, Facing perspective, Config config) {
4647
if (auth.getVersion() == AuthVersion.V2)
47-
return authenticateV2((Credentials) auth.unwrap(), endpoint, perspective, false, config);
48+
return authenticateV2((Auth) auth.unwrap(), endpoint, perspective, false, config);
4849

4950
return authenticateV3((KeystoneAuth) auth.unwrap(), endpoint, perspective, config);
5051
}
@@ -108,6 +109,9 @@ private static OSClient authenticateV2(Auth auth, String endpoint, Facing perspe
108109
if (auth.getType() == Type.CREDENTIALS) {
109110
access = access.applyContext(endpoint, (Credentials) auth);
110111
}
112+
else if (auth.getType() == Type.RAX_APIKEY) {
113+
access = access.applyContext(endpoint, (RaxApiKeyCredentials) auth);
114+
}
111115
else {
112116
access = access.applyContext(endpoint, (TokenAuth) auth);
113117
}

0 commit comments

Comments
 (0)