Skip to content

Commit

Permalink
Merge pull request #156 from tfelix/feature/keystone3-auth-scoping
Browse files Browse the repository at this point in the history
feat: Adds support for KeystoneV3 scoping during auth
  • Loading branch information
robert-bor authored May 20, 2019
2 parents 33902ea + 7c7b948 commit 0a5707c
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,26 @@ public class AccountConfig {
*/
private AuthenticationMethod authenticationMethod = KEYSTONE;

private AuthenticationMethod.AccessProvider accessProvider = null ;
private AuthenticationMethod.AccessProvider accessProvider = null;

/**
* Sets the scope of authentication when using the {@link AuthenticationMethod} Keystone V3.
* During authentication you can request a scope against a certain domain or project. Which might be required by
* your Keystone installation.
* <ul>
* <li>
* <b>DEFAULT</b>; Default authentication with no special scope. This is also the default setting.
* </li>
* <li>
* <b>PROJECT_NAME</b>; Scopes against a tenant/project name. You must also provide the TenantName.
* </li>
* <li>
* <b>DOMAIN_NAME</b>; Scopes against a domain name. You must also provide the Domain.
* </li>
*
* </ul>
*/
private AuthenticationMethodScope authenticationMethodScope = AuthenticationMethodScope.DEFAULT;

/**
* Keystone domain. Used by the V3 Identity API
Expand Down Expand Up @@ -485,4 +504,10 @@ public String getDomain() {
public void setDomain(String domain) {
this.domain = domain;
}

public AuthenticationMethodScope getAuthenticationMethodScope() { return authenticationMethodScope; }

public void setAuthenticationMethodScope(AuthenticationMethodScope scope) {
this.authenticationMethodScope = scope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,9 @@ public AccountFactory setDomain(String domain) {
this.config.setDomain(domain);
return this;
}

public AccountFactory setAuthenticationMode(AuthenticationMethodScope scope) {
this.config.setAuthenticationMethodScope(scope);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.javaswift.joss.client.factory;

/**
* The Keystone V3 API allows scoped authentication. By utilizing this option you can request a special scope during
* authentication.
*
* Depending on the chosen AuthenticationMethodScope different options must be provided in the {@link AccountConfig}
* object:
*
* <p>PROJECT_NAME: TenantName and Domain must be provided.</p>
* <p>DOMAIN_NAME: Domain must be provided.</p>
*
* <b>Note:</b> Currently only project/tenant name and domain name are supported. Scoping to the project/tenant ID or
* domain ID is not implemented.
*/
public enum AuthenticationMethodScope {
DEFAULT,
PROJECT_NAME,
DOMAIN_NAME
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
import org.javaswift.joss.exception.CommandException;
import org.javaswift.joss.headers.Header;
Expand Down Expand Up @@ -137,6 +138,7 @@ protected ObjectMapper createObjectMapper(boolean dealWithRootValue) {
if (dealWithRootValue) {
objectMapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
}
return objectMapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusSuccessCondition;
import org.javaswift.joss.command.shared.identity.AuthenticationCommand;
import org.javaswift.joss.command.shared.identity.access.KeystoneV3Access;
import org.javaswift.joss.command.shared.identity.authentication.KeystoneV3Authentication;
import org.javaswift.joss.command.shared.identity.authentication.*;
import org.javaswift.joss.exception.CommandException;
import org.javaswift.joss.headers.Accept;
import org.javaswift.joss.model.Access;
Expand All @@ -35,16 +35,60 @@ public KeystoneAuthenticationV3CommandImpl(HttpClient httpClient, AccountConfig
this.responseMapper = createObjectMapper(false);

setHeader(new Accept(ContentType.APPLICATION_JSON.getMimeType()));
setRequestBody(config.getUsername(), config.getPassword(), config.getDomain());

KeystoneV3Authentication auth = null;
switch (config.getAuthenticationMethodScope()) {
case DOMAIN_NAME:
auth = createKeystoneV3AuthenticationDomainScope(
config.getUsername(),
config.getPassword(),
config.getDomain()
);
break;
case PROJECT_NAME:
auth = createKeystoneV3AuthenticationProjectScope(
config.getUsername(),
config.getPassword(),
config.getDomain(),
config.getTenantName()
);
break;
default:
auth = createKeystoneV3AuthenticationDefaultScope(
config.getUsername(),
config.getPassword(),
config.getDomain()
);
break;
}
setRequestBody(auth);
}

private void setRequestBody(String username, String password, String domain) {
private KeystoneV3Authentication createKeystoneV3AuthenticationDefaultScope(String username, String password, String domain) {
KeystoneV3Authentication auth = new KeystoneV3Authentication();
auth.setIdentity(new KeystoneV3Identity(username, password, domain));
return auth;
}

private KeystoneV3Authentication createKeystoneV3AuthenticationProjectScope(String username, String password, String domain, String project) {
KeystoneV3Authentication auth = new KeystoneV3Authentication();
auth.setIdentity(new KeystoneV3Identity(username, password, domain));
auth.setScope(new KeystoneV3ProjectScope(project, new KeystoneV3Domain(domain)));
return auth;
}

private KeystoneV3Authentication createKeystoneV3AuthenticationDomainScope(String username, String password, String domain) {
KeystoneV3Authentication auth = new KeystoneV3Authentication();
auth.setIdentity(new KeystoneV3Identity(username, password, domain));
auth.setScope(new KeystoneV3DomainScope(new KeystoneV3Domain(domain)));
return auth;
}

private void setRequestBody(KeystoneV3Authentication auth) {
try {
String jsonString = requestMapper.writeValueAsString(
new KeystoneV3Authentication(username, password, domain)
);
String jsonString = requestMapper.writeValueAsString(auth);
request.setEntity(
new StringEntity(jsonString, ContentType.APPLICATION_JSON)
new StringEntity(jsonString, ContentType.APPLICATION_JSON)
);
} catch (IOException ex) {
throw new CommandException("Unable to set the JSON body on the request", ex);
Expand All @@ -67,8 +111,8 @@ protected HttpPost createRequest(String url) {

@Override
public HttpStatusChecker[] getStatusCheckers() {
return new HttpStatusChecker[] {
new HttpStatusSuccessCondition(new HttpStatusRange(200, 299))
return new HttpStatusChecker[]{
new HttpStatusSuccessCondition(new HttpStatusRange(200, 299))
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
public class KeystoneV3Authentication {

private KeystoneV3Identity identity;
private KeystoneV3Scope scope;

public KeystoneV3Authentication(String username, String password, String domain) {
this.identity = new KeystoneV3Identity(username, password, domain);
}
public void setScope(KeystoneV3Scope scope) { this.scope = scope; }

public KeystoneV3Scope getScope() { return scope; }

public void setIdentity(KeystoneV3Identity identity) { this.identity = identity; }

public KeystoneV3Identity getIdentity() {
return identity;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.javaswift.joss.command.shared.identity.authentication;

public class KeystoneV3DomainScope implements KeystoneV3Scope {

private final KeystoneV3Domain domain;

public KeystoneV3DomainScope(KeystoneV3Domain domain) {
this.domain = domain;
}

public KeystoneV3Domain getDomain() {
return domain;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.javaswift.joss.command.shared.identity.authentication;

public class KeystoneV3ProjectScope implements KeystoneV3Scope {

private KeystoneV3ProjectWrapped project;

public static class KeystoneV3ProjectWrapped {
private final String name;
private final KeystoneV3Domain domain;

private KeystoneV3ProjectWrapped(String name, KeystoneV3Domain domain) {
this.name = name;
this.domain = domain;
}

public String getName() {
return name;
}

public KeystoneV3Domain getDomain() {
return domain;
}
}

public KeystoneV3ProjectScope(String name, KeystoneV3Domain domain) {
this.project = new KeystoneV3ProjectWrapped(name, domain);
}

public KeystoneV3ProjectWrapped getProject() {
return project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.javaswift.joss.command.shared.identity.authentication;

public interface KeystoneV3Scope {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void construct() {
config.setPreferredRegion("AMS-01");
config.setDisableSslValidation(true);
config.setDelimiter('\\');
config.setAuthenticationMethodScope(AuthenticationMethodScope.DOMAIN_NAME);
assertEquals("auth", config.getAuthUrl());
assertEquals("pwd", config.getPassword());
assertEquals("tenant", config.getTenantName());
Expand All @@ -56,5 +57,6 @@ public void construct() {
assertEquals("AMS-01", config.getPreferredRegion());
assertEquals(true, config.isDisableSslValidation());
assertEquals((Character)'\\', config.getDelimiter());
assertEquals(AuthenticationMethodScope.DOMAIN_NAME, config.getAuthenticationMethodScope());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void useFluentInterface() {
.setTenantName(null)
.setTenantId(null)
.setUsername(null)
.setDelimiter('\\');
.setDelimiter('\\')
.setAuthenticationMethod(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -12,15 +13,13 @@
public class KeystoneV3AuthenticationTest {

@Test
public void testMarshalling() throws IOException {
public void testMarshallingDefaultScope() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

KeystoneV3Authentication authentication = new KeystoneV3Authentication(
"username",
"password",
"domainName"
);
KeystoneV3Authentication authentication = new KeystoneV3Authentication();
authentication.setIdentity(new KeystoneV3Identity("username", "password", "domainName"));

JsonNode expectedContent = mapper.readTree(
getClass().getResourceAsStream("/sample-v3-auth-request.json")
Expand All @@ -31,4 +30,42 @@ public void testMarshalling() throws IOException {
assertEquals(expectedContent, actualContent);
}

@Test
public void testMarshallingDomainScope() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

KeystoneV3Authentication authentication = new KeystoneV3Authentication();
authentication.setIdentity(new KeystoneV3Identity("username", "password", "domainName"));
authentication.setScope(new KeystoneV3DomainScope(new KeystoneV3Domain("domainName")));

JsonNode expectedContent = mapper.readTree(
getClass().getResourceAsStream("/sample-v3-auth-scope-domain-request.json")
);

JsonNode actualContent = mapper.valueToTree(authentication);

assertEquals(expectedContent, actualContent);
}

@Test
public void testMarshallingProjectScope() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

KeystoneV3Authentication authentication = new KeystoneV3Authentication();
authentication.setIdentity(new KeystoneV3Identity("username", "password", "domainName"));
authentication.setScope(new KeystoneV3ProjectScope("projectName", new KeystoneV3Domain("domainName")));

JsonNode expectedContent = mapper.readTree(
getClass().getResourceAsStream("/sample-v3-auth-scope-project-request.json")
);

JsonNode actualContent = mapper.valueToTree(authentication);

assertEquals(expectedContent, actualContent);
}

}
23 changes: 23 additions & 0 deletions src/test/resources/sample-v3-auth-scope-domain-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "username",
"password": "password",
"domain": {
"name": "domainName"
}
}
}
},
"scope":{
"domain":{
"name":"domainName"
}
}
}
}
26 changes: 26 additions & 0 deletions src/test/resources/sample-v3-auth-scope-project-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "username",
"password": "password",
"domain": {
"name": "domainName"
}
}
}
},
"scope":{
"project":{
"name":"projectName",
"domain":{
"name":"domainName"
}
}
}
}
}

0 comments on commit 0a5707c

Please sign in to comment.