From ea6b3dff65e125a8a2d4dc58b19d0342f18dd9b3 Mon Sep 17 00:00:00 2001 From: alpegon Date: Mon, 13 Jun 2016 15:54:51 +0200 Subject: [PATCH 1/5] Add amazon ec2 credentials builder and test --- .../grycap/im/auth/AuthorizationHeader.java | 8 ++--- .../{ => dummy}/DummyCredential.java | 5 ++- .../ec2/AmazonEc2UserPwdCredential.java | 34 +++++++++++++++++++ .../im/auth/credential/im/ImCredential.java | 9 ++--- .../im/auth/AuthorizationHeaderTest.java | 32 +++++++++++------ 5 files changed, 68 insertions(+), 20 deletions(-) rename src/main/java/es/upv/i3m/grycap/im/auth/credential/{ => dummy}/DummyCredential.java (77%) create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java b/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java index e704e60..c186814 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java @@ -9,16 +9,16 @@ public class AuthorizationHeader { private static final String ERROR_MESSAGE = "Credentials must not be null"; - private List> credentials = new ArrayList<>(); + private List credentials = new ArrayList<>(); - public List> getCredentials() { + public List getCredentials() { return credentials; } /** * Sets the credentials information. */ - public void setCredentialsAuthInfos(List> credentials) { + public void setCredentialsAuthInfos(List credentials) { if (credentials == null) { throw new IllegalArgumentException(ERROR_MESSAGE); } @@ -34,7 +34,7 @@ public void addCredential(Credential credential) { */ public String serialize() { StringBuilder sb = new StringBuilder(); - Iterator> it = credentials.iterator(); + Iterator it = credentials.iterator(); while (it.hasNext()) { String serializedAuthInfo = it.next().serialize(); sb.append(serializedAuthInfo); diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java similarity index 77% rename from src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java rename to src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java index f1d9912..ae1cadc 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java @@ -1,4 +1,7 @@ -package es.upv.i3m.grycap.im.auth.credential; +package es.upv.i3m.grycap.im.auth.credential.dummy; + +import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; public class DummyCredential extends AbstractCredential { diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java new file mode 100644 index 0000000..8a29848 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java @@ -0,0 +1,34 @@ +package es.upv.i3m.grycap.im.auth.credential.ec2; + +import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class AmazonEc2UserPwdCredential + extends AbstractUsernamePasswordCredential { + + protected AmazonEc2UserPwdCredential( + AmazonEc2UserPwdCredentialBuilder builder) { + super(builder); + } + + @Override + public ServiceType getServiceType() { + return ServiceType.EC2; + } + + public static AmazonEc2UserPwdCredentialBuilder getBuilder() { + return new AmazonEc2UserPwdCredentialBuilder(); + } + + //@formatter:off + public static class AmazonEc2UserPwdCredentialBuilder extends + AbstractUsernamePasswordCredentialBuilder { + //@formatter:on + + @Override + public AmazonEc2UserPwdCredential build() { + return new AmazonEc2UserPwdCredential(this); + } + } +} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java index c584973..c187144 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java @@ -35,16 +35,17 @@ public ImTokenCredential build() { public static class ImUsernamePasswordCredential extends AbstractUsernamePasswordCredential { - @Override - public ServiceType getServiceType() { - return ServiceType.INFRASTRUCTURE_MANAGER; - } protected ImUsernamePasswordCredential( ImUsernamePasswordCredentialBuilder builder) { super(builder); } + @Override + public ServiceType getServiceType() { + return ServiceType.INFRASTRUCTURE_MANAGER; + } + public static ImUsernamePasswordCredentialBuilder getBuilder() { return new ImUsernamePasswordCredentialBuilder(); } diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java index 9903e39..5857f4b 100644 --- a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java +++ b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java @@ -6,7 +6,8 @@ import es.upv.i3m.grycap.im.InfrastructureManager; import es.upv.i3m.grycap.im.InfrastructureManagerTest; import es.upv.i3m.grycap.im.auth.credential.Credential; -import es.upv.i3m.grycap.im.auth.credential.DummyCredential; +import es.upv.i3m.grycap.im.auth.credential.dummy.DummyCredential; +import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImTokenCredential; import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImUsernamePasswordCredential; import es.upv.i3m.grycap.im.auth.credential.occi.OcciCredential; import es.upv.i3m.grycap.im.auth.credential.opennebula.OpenNebulaTokenCredential; @@ -30,8 +31,10 @@ public class AuthorizationHeaderTest extends ImTestWatcher { private static final String DUMMY_CREDS = "id = dummy ; type = Dummy"; - private static final String IM_CREDS = + private static final String IM_UP_CREDS = "type = InfrastructureManager ; username = imuser01 ; password = pwd"; + private static final String IM_TK_CREDS = + "type = InfrastructureManager ; token = token"; private static final String VMRC_CREDS = "type = VMRC ; username = demo ; password = pwd ; host = host"; private static final String OST_CREDS = @@ -56,26 +59,25 @@ public static void createAuthorizationHeader() { @Before public void clearAuthorizationHeader() { - ah.setCredentialsAuthInfos(new ArrayList>()); + ah.setCredentialsAuthInfos(new ArrayList()); } @Test public void testAuthorizationHeader() throws ImClientException { // Create authorization headers - AuthorizationHeader authorizationHeader = new AuthorizationHeader(); Credential cred = ImUsernamePasswordCredential.getBuilder() .withUsername("imuser01").withPassword("invitado").build(); - authorizationHeader.addCredential(cred); + ah.addCredential(cred); cred = VmrcCredential.getBuilder().withUsername("demo").withPassword("demo") .withHost("http://servproject.i3m.upv.es:8080/vmrc/vmrc").build(); - authorizationHeader.addCredential(cred); + ah.addCredential(cred); cred = DummyCredential.getBuilder().withId("dummy").build(); - authorizationHeader.addCredential(cred); + ah.addCredential(cred); // Check the headers work with the dummy provider try { - InfrastructureManager im = new InfrastructureManager( - IM_DUMMY_PROVIDER_URL, authorizationHeader.serialize()); + InfrastructureManager im = + new InfrastructureManager(IM_DUMMY_PROVIDER_URL, ah.serialize()); String toscaFile = new NoNullOrEmptyFile(new Utf8File(Paths.get(TOSCA_FILE_PATH))) .read(); @@ -101,11 +103,19 @@ public void testDummyCredentials() throws ImClientException { } @Test - public void testImCredentials() throws ImClientException { + public void testImUserPassCredentials() throws ImClientException { Credential cred = ImUsernamePasswordCredential.getBuilder() .withUsername("imuser01").withPassword("pwd").build(); ah.addCredential(cred); - Assert.assertEquals(IM_CREDS, ah.serialize()); + Assert.assertEquals(IM_UP_CREDS, ah.serialize()); + } + + @Test + public void testImTokenCredentials() throws ImClientException { + Credential cred = + ImTokenCredential.getBuilder().withToken("token").build(); + ah.addCredential(cred); + Assert.assertEquals(IM_TK_CREDS, ah.serialize()); } @Test From 299dc56d2125b21f93acab61c234cd8cee696caa Mon Sep 17 00:00:00 2001 From: alpegon Date: Mon, 13 Jun 2016 16:08:03 +0200 Subject: [PATCH 2/5] Add Docker credential builder and test --- .../credential/docker/DockerCredential.java | 61 +++++++++++++++++++ .../im/auth/AuthorizationHeaderTest.java | 38 +++++++++--- 2 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java new file mode 100644 index 0000000..dd72689 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java @@ -0,0 +1,61 @@ +package es.upv.i3m.grycap.im.auth.credential.docker; + +import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class DockerCredential extends AbstractCredential { + + private String host; + + protected DockerCredential(DockerCredentialBuilder builder) { + super(builder); + setHost(builder.getHost()); + } + + public String getHost() { + return host; + } + + private void setHost(String host) { + if (isNullOrEmpty(host)) { + throw new IllegalArgumentException("Host must not be blank"); + } + this.host = host; + } + + @Override + public ServiceType getServiceType() { + return ServiceType.DOCKER; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; host = ").append(host); + return sb; + } + + public static DockerCredentialBuilder getBuilder() { + return new DockerCredentialBuilder(); + } + + public static class DockerCredentialBuilder extends + AbstractCredentialBuilder { + + private String host; + + public String getHost() { + return host; + } + + public DockerCredentialBuilder withHost(String host) { + this.host = host; + return this; + } + + @Override + public DockerCredential build() { + return new DockerCredential(this); + } + } +} \ No newline at end of file diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java index 5857f4b..3171873 100644 --- a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java +++ b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java @@ -6,7 +6,9 @@ import es.upv.i3m.grycap.im.InfrastructureManager; import es.upv.i3m.grycap.im.InfrastructureManagerTest; import es.upv.i3m.grycap.im.auth.credential.Credential; +import es.upv.i3m.grycap.im.auth.credential.docker.DockerCredential; import es.upv.i3m.grycap.im.auth.credential.dummy.DummyCredential; +import es.upv.i3m.grycap.im.auth.credential.ec2.AmazonEc2UserPwdCredential; import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImTokenCredential; import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImUsernamePasswordCredential; import es.upv.i3m.grycap.im.auth.credential.occi.OcciCredential; @@ -49,6 +51,10 @@ public class AuthorizationHeaderTest extends ImTestWatcher { "http://servproject.i3m.upv.es:8811"; private static final String TOSCA_FILE_PATH = "./src/test/resources/tosca/galaxy_tosca.yaml"; + private static final String EC2_CREDS = + "id = ec2 ; type = EC2 ; username = demo ; password = pwd"; + private static final String DOCKER_CREDS = + "id = docker ; type = Docker ; host = host_url"; private static AuthorizationHeader ah; @@ -96,14 +102,14 @@ public void testAuthorizationHeader() throws ImClientException { } @Test - public void testDummyCredentials() throws ImClientException { + public void testDummyCredentials() { Credential cred = DummyCredential.getBuilder().withId("dummy").build(); ah.addCredential(cred); Assert.assertEquals(DUMMY_CREDS, ah.serialize()); } @Test - public void testImUserPassCredentials() throws ImClientException { + public void testImUserPassCredentials() { Credential cred = ImUsernamePasswordCredential.getBuilder() .withUsername("imuser01").withPassword("pwd").build(); ah.addCredential(cred); @@ -111,7 +117,7 @@ public void testImUserPassCredentials() throws ImClientException { } @Test - public void testImTokenCredentials() throws ImClientException { + public void testImTokenCredentials() { Credential cred = ImTokenCredential.getBuilder().withToken("token").build(); ah.addCredential(cred); @@ -119,7 +125,7 @@ public void testImTokenCredentials() throws ImClientException { } @Test - public void testVmrcCredentials() throws ImClientException { + public void testVmrcCredentials() { Credential cred = VmrcCredential.getBuilder().withUsername("demo") .withPassword("pwd").withHost("host").build(); ah.addCredential(cred); @@ -127,7 +133,7 @@ public void testVmrcCredentials() throws ImClientException { } @Test - public void testOpenStackCredentials() throws ImClientException { + public void testOpenStackCredentials() { Credential cred = OpenstackCredential.getBuilder().withId("ost") .withUsername("usr").withPassword("pwd").withTenant("tenant") .withServiceRegion("region").withHost("host") @@ -137,7 +143,7 @@ public void testOpenStackCredentials() throws ImClientException { } @Test - public void testOpenNebulaUserPassCredentials() throws ImClientException { + public void testOpenNebulaUserPassCredentials() { Credential cred = OpenNebulaUserPwdCredential.getBuilder().withId("one") .withUsername("usr").withPassword("pwd").withHost("host").build(); ah.addCredential(cred); @@ -145,7 +151,7 @@ public void testOpenNebulaUserPassCredentials() throws ImClientException { } @Test - public void testOpenNebulaTokenCredentials() throws ImClientException { + public void testOpenNebulaTokenCredentials() { Credential cred = OpenNebulaTokenCredential.getBuilder().withId("one") .withToken("token").withHost("host").build(); ah.addCredential(cred); @@ -153,11 +159,27 @@ public void testOpenNebulaTokenCredentials() throws ImClientException { } @Test - public void testOcciCredentials() throws ImClientException { + public void testOcciCredentials() { Credential cred = OcciCredential.getBuilder().withId("occi") .withHost("host").withProxy("proxy").build(); ah.addCredential(cred); Assert.assertEquals(OCCI_CREDS, ah.serialize()); } + @Test + public void testAmazonEc2UserPassCredentials() { + Credential cred = AmazonEc2UserPwdCredential.getBuilder().withId("ec2") + .withUsername("demo").withPassword("pwd").build(); + ah.addCredential(cred); + Assert.assertEquals(EC2_CREDS, ah.serialize()); + } + + @Test + public void testDockerCredentials() { + Credential cred = DockerCredential.getBuilder().withId("docker") + .withHost("host_url").build(); + ah.addCredential(cred); + Assert.assertEquals(DOCKER_CREDS, ah.serialize()); + } + } From fec3c6f9ed98ee08b55464288b8ce212329cfde3 Mon Sep 17 00:00:00 2001 From: alpegon Date: Mon, 13 Jun 2016 16:34:11 +0200 Subject: [PATCH 3/5] Add GCE credential builder and test --- .../credential/gce/GceUserPwdCredential.java | 65 +++++++++++++++++++ .../OpenNebulaUserPwdCredential.java | 12 ++-- .../im/auth/AuthorizationHeaderTest.java | 48 +++++++++----- 3 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java new file mode 100644 index 0000000..534b210 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java @@ -0,0 +1,65 @@ +package es.upv.i3m.grycap.im.auth.credential.gce; + +import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class GceUserPwdCredential + extends AbstractUsernamePasswordCredential { + + private String project; + + protected GceUserPwdCredential(GceUserPwdCredentialBuilder builder) { + super(builder); + setProject(builder.getProject()); + } + + public String getProject() { + return project; + } + + private void setProject(String project) { + if (isNullOrEmpty(project)) { + throw new IllegalArgumentException("Project must not be blank"); + } + this.project = project; + } + + @Override + public ServiceType getServiceType() { + return ServiceType.GCE; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; project = ").append(project); + return sb; + } + + public static GceUserPwdCredentialBuilder getBuilder() { + return new GceUserPwdCredentialBuilder(); + } + + //@formatter:off + public static class GceUserPwdCredentialBuilder extends + AbstractUsernamePasswordCredentialBuilder { + //@formatter:on + + private String project; + + public String getProject() { + return project; + } + + public GceUserPwdCredentialBuilder withProject(String project) { + this.project = project; + return this; + } + + @Override + public GceUserPwdCredential build() { + return new GceUserPwdCredential(this); + } + } +} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java index 59980c1..59213a1 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java @@ -8,6 +8,12 @@ public class OpenNebulaUserPwdCredential private String host; + protected OpenNebulaUserPwdCredential( + OpennebulaUserPwdCredentialBuilder builder) { + super(builder); + setHost(builder.getHost()); + } + public String getHost() { return host; } @@ -31,12 +37,6 @@ public StringBuilder serialize(StringBuilder sb) { return sb; } - protected OpenNebulaUserPwdCredential( - OpennebulaUserPwdCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - } - public static OpennebulaUserPwdCredentialBuilder getBuilder() { return new OpennebulaUserPwdCredentialBuilder(); } diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java index 3171873..02696ab 100644 --- a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java +++ b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java @@ -9,6 +9,7 @@ import es.upv.i3m.grycap.im.auth.credential.docker.DockerCredential; import es.upv.i3m.grycap.im.auth.credential.dummy.DummyCredential; import es.upv.i3m.grycap.im.auth.credential.ec2.AmazonEc2UserPwdCredential; +import es.upv.i3m.grycap.im.auth.credential.gce.GceUserPwdCredential; import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImTokenCredential; import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImUsernamePasswordCredential; import es.upv.i3m.grycap.im.auth.credential.occi.OcciCredential; @@ -32,29 +33,38 @@ public class AuthorizationHeaderTest extends ImTestWatcher { + // Test user/pass + private static final String USER = "user"; + private static final String PASS = "pass"; + private static final String USER_PASS = + "username = " + USER + " ; password = " + PASS; + // Credentials private static final String DUMMY_CREDS = "id = dummy ; type = Dummy"; private static final String IM_UP_CREDS = - "type = InfrastructureManager ; username = imuser01 ; password = pwd"; + "type = InfrastructureManager ; " + USER_PASS; private static final String IM_TK_CREDS = "type = InfrastructureManager ; token = token"; private static final String VMRC_CREDS = - "type = VMRC ; username = demo ; password = pwd ; host = host"; - private static final String OST_CREDS = - "id = ost ; type = OpenStack ; username = usr ; password = pwd ; host = host ; service_region = region"; + "type = VMRC ; " + USER_PASS + " ; host = host"; + private static final String OST_CREDS = "id = ost ; type = OpenStack ; " + + USER_PASS + " ; host = host ; service_region = region"; private static final String OCCI_CREDS = "id = occi ; type = OCCI ; host = host ; proxy = proxy"; private static final String ONE_UP_CREDS = - "id = one ; type = OpenNebula ; username = usr ; password = pwd ; host = host"; + "id = one ; type = OpenNebula ; " + USER_PASS + " ; host = host"; private static final String ONE_TK_CREDS = "id = one ; type = OpenNebula ; token = token ; host = host"; + private static final String EC2_CREDS = + "id = ec2 ; type = EC2 ; " + USER_PASS; + private static final String DOCKER_CREDS = + "id = docker ; type = Docker ; host = host_url"; + private static final String GCE_CREDS = + "id = gce ; type = GCE ; " + USER_PASS + " ; project = testPrj"; + // IM information private static final String IM_DUMMY_PROVIDER_URL = "http://servproject.i3m.upv.es:8811"; private static final String TOSCA_FILE_PATH = "./src/test/resources/tosca/galaxy_tosca.yaml"; - private static final String EC2_CREDS = - "id = ec2 ; type = EC2 ; username = demo ; password = pwd"; - private static final String DOCKER_CREDS = - "id = docker ; type = Docker ; host = host_url"; private static AuthorizationHeader ah; @@ -111,7 +121,7 @@ public void testDummyCredentials() { @Test public void testImUserPassCredentials() { Credential cred = ImUsernamePasswordCredential.getBuilder() - .withUsername("imuser01").withPassword("pwd").build(); + .withUsername(USER).withPassword(PASS).build(); ah.addCredential(cred); Assert.assertEquals(IM_UP_CREDS, ah.serialize()); } @@ -126,8 +136,8 @@ public void testImTokenCredentials() { @Test public void testVmrcCredentials() { - Credential cred = VmrcCredential.getBuilder().withUsername("demo") - .withPassword("pwd").withHost("host").build(); + Credential cred = VmrcCredential.getBuilder().withUsername(USER) + .withPassword(PASS).withHost("host").build(); ah.addCredential(cred); Assert.assertEquals(VMRC_CREDS, ah.serialize()); } @@ -135,7 +145,7 @@ public void testVmrcCredentials() { @Test public void testOpenStackCredentials() { Credential cred = OpenstackCredential.getBuilder().withId("ost") - .withUsername("usr").withPassword("pwd").withTenant("tenant") + .withUsername(USER).withPassword(PASS).withTenant("tenant") .withServiceRegion("region").withHost("host") .withAuthVersion(OpenstackAuthVersion.PASSWORD_2_0).build(); ah.addCredential(cred); @@ -145,7 +155,7 @@ public void testOpenStackCredentials() { @Test public void testOpenNebulaUserPassCredentials() { Credential cred = OpenNebulaUserPwdCredential.getBuilder().withId("one") - .withUsername("usr").withPassword("pwd").withHost("host").build(); + .withUsername(USER).withPassword(PASS).withHost("host").build(); ah.addCredential(cred); Assert.assertEquals(ONE_UP_CREDS, ah.serialize()); } @@ -169,7 +179,7 @@ public void testOcciCredentials() { @Test public void testAmazonEc2UserPassCredentials() { Credential cred = AmazonEc2UserPwdCredential.getBuilder().withId("ec2") - .withUsername("demo").withPassword("pwd").build(); + .withUsername(USER).withPassword(PASS).build(); ah.addCredential(cred); Assert.assertEquals(EC2_CREDS, ah.serialize()); } @@ -182,4 +192,12 @@ public void testDockerCredentials() { Assert.assertEquals(DOCKER_CREDS, ah.serialize()); } + @Test + public void testGceUserPwdCredentials() { + Credential cred = GceUserPwdCredential.getBuilder().withId("gce") + .withUsername(USER).withPassword(PASS).withProject("testPrj").build(); + ah.addCredential(cred); + Assert.assertEquals(GCE_CREDS, ah.serialize()); + } + } From 09645aa1ebc40745ed678b6170ba694e47a78d1c Mon Sep 17 00:00:00 2001 From: alpegon Date: Mon, 13 Jun 2016 17:11:36 +0200 Subject: [PATCH 4/5] Update code to simplify credentials creation The id field is automatically included by the builder, the user doesn't need to specify it. --- .../auth/credential/AbstractCredential.java | 45 +++++-------------- .../credential/AbstractTokenCredential.java | 11 ++--- .../AbstractUsernamePasswordCredential.java | 19 ++++---- .../grycap/im/auth/credential/Credential.java | 2 +- .../im/auth/credential/ServiceProvider.java | 36 +++++++++++++++ .../im/auth/credential/ServiceType.java | 32 ------------- .../credential/docker/DockerCredential.java | 19 ++++---- .../credential/dummy/DummyCredential.java | 6 +-- .../ec2/AmazonEc2UserPwdCredential.java | 6 +-- .../credential/gce/GceUserPwdCredential.java | 6 +-- .../im/auth/credential/im/ImCredential.java | 10 ++--- .../auth/credential/occi/OcciCredential.java | 6 +-- .../opennebula/OpenNebulaTokenCredential.java | 6 +-- .../OpenNebulaUserPwdCredential.java | 6 +-- .../openstack/OpenstackCredential.java | 6 +-- .../auth/credential/vmrc/VmrcCredential.java | 6 +-- .../im/auth/AuthorizationHeaderTest.java | 36 +++++++-------- 17 files changed, 122 insertions(+), 136 deletions(-) create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java index dd30f64..8c12dbe 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java @@ -3,19 +3,8 @@ public abstract class AbstractCredential> implements Credential { - private String id; - protected > AbstractCredential( B builder) { - id = builder.getId(); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; } @Override @@ -23,15 +12,19 @@ public String serialize() { return serialize(null).toString(); } - protected StringBuilder serialize(StringBuilder sb) { - if (sb == null) { - sb = new StringBuilder(); + protected StringBuilder serialize(final StringBuilder sb) { + StringBuilder serializedSb = sb; + if (serializedSb == null) { + serializedSb = new StringBuilder(); } - if (!isNullOrEmpty(id)) { - sb.append("id = ").append(id).append(" ; "); - } - sb.append("type = ").append(getServiceType().getValue()); - return sb; + serializedSb.append("id = ").append(getServiceProvider().getId()) + .append(" ; "); + serializedSb.append("type = ").append(getServiceProvider().getType()); + return serializedSb; + } + + public static boolean isNullOrEmpty(String string) { + return (string == null) || string.isEmpty(); } //@formatter:off @@ -40,20 +33,6 @@ public abstract static class AbstractCredentialBuilder implements CredentialBuilder { //@formatter:on - private String id; - - public String getId() { - return id; - } - - @SuppressWarnings("unchecked") - public B withId(String id) { - this.id = id; - return (B) this; - } } - public static boolean isNullOrEmpty(String string) { - return (string == null) || string.isEmpty(); - } } diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java index 2dfcfff..ad714ab 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java @@ -17,16 +17,17 @@ public String getToken() { private void setToken(String token) { if (isNullOrEmpty(token)) { - throw new IllegalArgumentException("token must not be blank"); + throw new IllegalArgumentException("Token must not be blank"); } this.token = token; } @Override - protected StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; token = ").append(token); - return sb; + protected StringBuilder serialize(final StringBuilder sb) { + StringBuilder serializedSb = sb; + serializedSb = super.serialize(serializedSb); + serializedSb.append(" ; token = ").append(token); + return serializedSb; } //@formatter:off diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java index 8af35d6..b5c94a4 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java @@ -23,7 +23,7 @@ public String getUsername() { return username; } - public void setUsername(String username) { + public void setUsername(final String username) { this.username = username; } @@ -31,20 +31,21 @@ public String getPassword() { return password; } - public void setPassword(String password) { + public void setPassword(final String password) { this.password = password; } @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); + public StringBuilder serialize(final StringBuilder sb) { + StringBuilder serializedSb = sb; + serializedSb = super.serialize(serializedSb); if (!isNullOrEmpty(username)) { - sb.append(" ; username = ").append(username); + serializedSb.append(" ; username = ").append(username); } if (!isNullOrEmpty(password)) { - sb.append(" ; password = ").append(password); + serializedSb.append(" ; password = ").append(password); } - return sb; + return serializedSb; } //@formatter:off @@ -58,13 +59,13 @@ public abstract static class AbstractUsernamePasswordCredentialBuilder private String password; @SuppressWarnings("unchecked") - public B withUsername(String username) { + public B withUsername(final String username) { this.username = username; return (B) this; } @SuppressWarnings("unchecked") - public B withPassword(String password) { + public B withPassword(final String password) { this.password = password; return (B) this; } diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java index ea5a157..164d621 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java @@ -2,7 +2,7 @@ public interface Credential> { - public ServiceType getServiceType(); + public ServiceProvider getServiceProvider(); public String serialize(); diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java new file mode 100644 index 0000000..1bbfe6d --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java @@ -0,0 +1,36 @@ +package es.upv.i3m.grycap.im.auth.credential; + +public enum ServiceProvider { + + //@formatter:off + INFRASTRUCTURE_MANAGER("im","InfrastructureManager"), + VMRC("vmrc","VMRC"), + DUMMY("dummy","Dummy"), + OPENNEBULA("one","OpenNebula"), + EC2("ec2","EC2"), + FOG_BOW("fogbow","FogBow"), + OPENSTACK("ost","OpenStack"), + OCCI("occi","OCCI"), + DOCKER("docker","Docker"), + GCE("gce","GCE"), + AZURE("azure","Azure"), + KUBERNETES("kub","Kubernetes"); + //@formatter:on + + private final String id; + private final String type; + + private ServiceProvider(final String id, final String type) { + this.id = id; + this.type = type; + } + + public final String getId() { + return id; + } + + public final String getType() { + return type; + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java deleted file mode 100644 index 537246f..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java +++ /dev/null @@ -1,32 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -public enum ServiceType { - - //@formatter:off - INFRASTRUCTURE_MANAGER("InfrastructureManager"), - VMRC("VMRC"), - DUMMY("Dummy"), - OPENNEBULA("OpenNebula"), - EC2("EC2"), - FOG_BOW("FogBow"), - OPENSTACK("OpenStack"), - OCCI("OCCI"), - LIB_CLOUD("LibCloud"), - DOCKER("Docker"), - GCE("GCE"), - AZURE("Azure"), - KUBERNETES("Kubernetes"), - LIB_VIRT("LibVirt"); - //@formatter:on - - private final String value; - - ServiceType(String value) { - this.value = value; - } - - public final String getValue() { - return value; - } - -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java index dd72689..472b9ac 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.docker; import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class DockerCredential extends AbstractCredential { @@ -16,7 +16,7 @@ public String getHost() { return host; } - private void setHost(String host) { + private void setHost(final String host) { if (isNullOrEmpty(host)) { throw new IllegalArgumentException("Host must not be blank"); } @@ -24,15 +24,16 @@ private void setHost(String host) { } @Override - public ServiceType getServiceType() { - return ServiceType.DOCKER; + public ServiceProvider getServiceProvider() { + return ServiceProvider.DOCKER; } @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - return sb; + public StringBuilder serialize(final StringBuilder sb) { + StringBuilder serializedSb = sb; + serializedSb = super.serialize(serializedSb); + serializedSb.append(" ; host = ").append(host); + return serializedSb; } public static DockerCredentialBuilder getBuilder() { @@ -48,7 +49,7 @@ public String getHost() { return host; } - public DockerCredentialBuilder withHost(String host) { + public DockerCredentialBuilder withHost(final String host) { this.host = host; return this; } diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java index ae1cadc..f62b2c8 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.dummy; import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class DummyCredential extends AbstractCredential { @@ -10,8 +10,8 @@ protected DummyCredential(DummyCredentialBuilder builder) { } @Override - public ServiceType getServiceType() { - return ServiceType.DUMMY; + public ServiceProvider getServiceProvider() { + return ServiceProvider.DUMMY; } public static DummyCredentialBuilder getBuilder() { diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java index 8a29848..ef27f61 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.ec2; import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class AmazonEc2UserPwdCredential extends AbstractUsernamePasswordCredential { @@ -12,8 +12,8 @@ protected AmazonEc2UserPwdCredential( } @Override - public ServiceType getServiceType() { - return ServiceType.EC2; + public ServiceProvider getServiceProvider() { + return ServiceProvider.EC2; } public static AmazonEc2UserPwdCredentialBuilder getBuilder() { diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java index 534b210..5a57bf3 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.gce; import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class GceUserPwdCredential extends AbstractUsernamePasswordCredential { @@ -25,8 +25,8 @@ private void setProject(String project) { } @Override - public ServiceType getServiceType() { - return ServiceType.GCE; + public ServiceProvider getServiceProvider() { + return ServiceProvider.GCE; } @Override diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java index c187144..29e3ffe 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java @@ -2,7 +2,7 @@ import es.upv.i3m.grycap.im.auth.credential.AbstractTokenCredential; import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public final class ImCredential { @@ -14,8 +14,8 @@ protected ImTokenCredential(ImTokenCredentialBuilder builder) { } @Override - public ServiceType getServiceType() { - return ServiceType.INFRASTRUCTURE_MANAGER; + public ServiceProvider getServiceProvider() { + return ServiceProvider.INFRASTRUCTURE_MANAGER; } public static ImTokenCredentialBuilder getBuilder() { @@ -42,8 +42,8 @@ protected ImUsernamePasswordCredential( } @Override - public ServiceType getServiceType() { - return ServiceType.INFRASTRUCTURE_MANAGER; + public ServiceProvider getServiceProvider() { + return ServiceProvider.INFRASTRUCTURE_MANAGER; } public static ImUsernamePasswordCredentialBuilder getBuilder() { diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java index fa6bd00..18a878c 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.occi; import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class OcciCredential extends AbstractCredential { @@ -15,8 +15,8 @@ protected OcciCredential(OcciCredentialBuilder builder) { } @Override - public ServiceType getServiceType() { - return ServiceType.OCCI; + public ServiceProvider getServiceProvider() { + return ServiceProvider.OCCI; } public String getHost() { diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java index a52e1dd..2a960f1 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.opennebula; import es.upv.i3m.grycap.im.auth.credential.AbstractTokenCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class OpenNebulaTokenCredential extends AbstractTokenCredential { @@ -26,8 +26,8 @@ private void setHost(String host) { } @Override - public ServiceType getServiceType() { - return ServiceType.OPENNEBULA; + public ServiceProvider getServiceProvider() { + return ServiceProvider.OPENNEBULA; } @Override diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java index 59213a1..787e4db 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.opennebula; import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class OpenNebulaUserPwdCredential extends AbstractUsernamePasswordCredential { @@ -26,8 +26,8 @@ private void setHost(String host) { } @Override - public ServiceType getServiceType() { - return ServiceType.OPENNEBULA; + public ServiceProvider getServiceProvider() { + return ServiceProvider.OPENNEBULA; } @Override diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java index d69e148..10ff82b 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.openstack; import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class OpenstackCredential extends AbstractUsernamePasswordCredential { @@ -26,8 +26,8 @@ protected OpenstackCredential(OpenstackCredentialBuilder builder) { } @Override - public ServiceType getServiceType() { - return ServiceType.OPENSTACK; + public ServiceProvider getServiceProvider() { + return ServiceProvider.OPENSTACK; } public OpenstackAuthVersion getAuthVersion() { diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java index 506e964..3581b08 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java @@ -1,7 +1,7 @@ package es.upv.i3m.grycap.im.auth.credential.vmrc; import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceType; +import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; public class VmrcCredential extends AbstractUsernamePasswordCredential { @@ -15,8 +15,8 @@ protected VmrcCredential(VmrcCredentialBuilder builder) { } @Override - public ServiceType getServiceType() { - return ServiceType.VMRC; + public ServiceProvider getServiceProvider() { + return ServiceProvider.VMRC; } public String getHost() { diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java index 02696ab..06e7422 100644 --- a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java +++ b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java @@ -41,11 +41,11 @@ public class AuthorizationHeaderTest extends ImTestWatcher { // Credentials private static final String DUMMY_CREDS = "id = dummy ; type = Dummy"; private static final String IM_UP_CREDS = - "type = InfrastructureManager ; " + USER_PASS; + "id = im ; type = InfrastructureManager ; " + USER_PASS; private static final String IM_TK_CREDS = - "type = InfrastructureManager ; token = token"; + "id = im ; type = InfrastructureManager ; token = token"; private static final String VMRC_CREDS = - "type = VMRC ; " + USER_PASS + " ; host = host"; + "id = vmrc ; type = VMRC ; " + USER_PASS + " ; host = host"; private static final String OST_CREDS = "id = ost ; type = OpenStack ; " + USER_PASS + " ; host = host ; service_region = region"; private static final String OCCI_CREDS = @@ -87,7 +87,7 @@ public void testAuthorizationHeader() throws ImClientException { cred = VmrcCredential.getBuilder().withUsername("demo").withPassword("demo") .withHost("http://servproject.i3m.upv.es:8080/vmrc/vmrc").build(); ah.addCredential(cred); - cred = DummyCredential.getBuilder().withId("dummy").build(); + cred = DummyCredential.getBuilder().build(); ah.addCredential(cred); // Check the headers work with the dummy provider @@ -113,7 +113,7 @@ public void testAuthorizationHeader() throws ImClientException { @Test public void testDummyCredentials() { - Credential cred = DummyCredential.getBuilder().withId("dummy").build(); + Credential cred = DummyCredential.getBuilder().build(); ah.addCredential(cred); Assert.assertEquals(DUMMY_CREDS, ah.serialize()); } @@ -144,17 +144,17 @@ public void testVmrcCredentials() { @Test public void testOpenStackCredentials() { - Credential cred = OpenstackCredential.getBuilder().withId("ost") - .withUsername(USER).withPassword(PASS).withTenant("tenant") - .withServiceRegion("region").withHost("host") - .withAuthVersion(OpenstackAuthVersion.PASSWORD_2_0).build(); + Credential cred = + OpenstackCredential.getBuilder().withUsername(USER).withPassword(PASS) + .withTenant("tenant").withServiceRegion("region").withHost("host") + .withAuthVersion(OpenstackAuthVersion.PASSWORD_2_0).build(); ah.addCredential(cred); Assert.assertEquals(OST_CREDS, ah.serialize()); } @Test public void testOpenNebulaUserPassCredentials() { - Credential cred = OpenNebulaUserPwdCredential.getBuilder().withId("one") + Credential cred = OpenNebulaUserPwdCredential.getBuilder() .withUsername(USER).withPassword(PASS).withHost("host").build(); ah.addCredential(cred); Assert.assertEquals(ONE_UP_CREDS, ah.serialize()); @@ -162,7 +162,7 @@ public void testOpenNebulaUserPassCredentials() { @Test public void testOpenNebulaTokenCredentials() { - Credential cred = OpenNebulaTokenCredential.getBuilder().withId("one") + Credential cred = OpenNebulaTokenCredential.getBuilder() .withToken("token").withHost("host").build(); ah.addCredential(cred); Assert.assertEquals(ONE_TK_CREDS, ah.serialize()); @@ -170,15 +170,15 @@ public void testOpenNebulaTokenCredentials() { @Test public void testOcciCredentials() { - Credential cred = OcciCredential.getBuilder().withId("occi") - .withHost("host").withProxy("proxy").build(); + Credential cred = + OcciCredential.getBuilder().withHost("host").withProxy("proxy").build(); ah.addCredential(cred); Assert.assertEquals(OCCI_CREDS, ah.serialize()); } @Test public void testAmazonEc2UserPassCredentials() { - Credential cred = AmazonEc2UserPwdCredential.getBuilder().withId("ec2") + Credential cred = AmazonEc2UserPwdCredential.getBuilder() .withUsername(USER).withPassword(PASS).build(); ah.addCredential(cred); Assert.assertEquals(EC2_CREDS, ah.serialize()); @@ -186,16 +186,16 @@ public void testAmazonEc2UserPassCredentials() { @Test public void testDockerCredentials() { - Credential cred = DockerCredential.getBuilder().withId("docker") - .withHost("host_url").build(); + Credential cred = + DockerCredential.getBuilder().withHost("host_url").build(); ah.addCredential(cred); Assert.assertEquals(DOCKER_CREDS, ah.serialize()); } @Test public void testGceUserPwdCredentials() { - Credential cred = GceUserPwdCredential.getBuilder().withId("gce") - .withUsername(USER).withPassword(PASS).withProject("testPrj").build(); + Credential cred = GceUserPwdCredential.getBuilder().withUsername(USER) + .withPassword(PASS).withProject("testPrj").build(); ah.addCredential(cred); Assert.assertEquals(GCE_CREDS, ah.serialize()); } From 0e3c9172ae1cf91e9d209df310bdfee35f283835 Mon Sep 17 00:00:00 2001 From: alpegon Date: Wed, 15 Jun 2016 14:53:05 +0200 Subject: [PATCH 5/5] Refactored authorization header builders Simplified the creation of new properties for the headers and new builders Added new builders and the correspondant tests --- README.md | 16 +- pom.xml | 4 +- .../i3m/grycap/im/InfrastructureManager.java | 265 ++++++++++-------- .../auth/credential/AbstractCredential.java | 38 --- .../credential/AbstractTokenCredential.java | 50 ---- .../AbstractUsernamePasswordCredential.java | 82 ------ .../grycap/im/auth/credential/Credential.java | 14 - .../credential/docker/DockerCredential.java | 62 ---- .../credential/dummy/DummyCredential.java | 31 -- .../ec2/AmazonEc2UserPwdCredential.java | 34 --- .../credential/gce/GceUserPwdCredential.java | 65 ----- .../im/auth/credential/im/ImCredential.java | 65 ----- .../auth/credential/occi/OcciCredential.java | 85 ------ .../opennebula/OpenNebulaTokenCredential.java | 63 ----- .../OpenNebulaUserPwdCredential.java | 66 ----- .../openstack/OpenstackAuthVersion.java | 19 -- .../openstack/OpenstackCredential.java | 198 ------------- .../auth/credential/vmrc/VmrcCredential.java | 64 ----- .../AuthorizationHeader.java | 14 +- .../im/auth/credentials/Credentials.java | 10 + .../ServiceProvider.java | 4 +- .../properties/AuthTokenProperty.java | 15 + .../properties/BaseProperties.java | 27 ++ .../properties/BaseUrlProperty.java | 14 + .../properties/GenericProperty.java | 42 +++ .../credentials/properties/HostProperty.java | 14 + .../OpenStackAuthVersionProperty.java | 27 ++ .../properties/PasswordProperty.java | 14 + .../properties/PrivateKeyProperty.java | 14 + .../properties/ProjectProperty.java | 14 + .../credentials/properties/ProxyProperty.java | 14 + .../properties/PublicKeyProperty.java | 14 + .../properties/ServiceNameProperty.java | 14 + .../properties/ServiceRegionProperty.java | 15 + .../properties/TenantProperty.java | 14 + .../credentials/properties/TokenProperty.java | 14 + .../properties/UsernameProperty.java | 14 + .../providers/AmazonEc2Credentials.java | 16 ++ .../providers/AzureCredentials.java | 27 ++ .../providers/DockerCredentials.java | 15 + .../providers/DummyCredential.java | 15 + .../providers/FogBowCredentials.java | 15 + .../credentials/providers/GceCredentials.java | 21 ++ .../providers/GenericCredentials.java | 65 +++++ .../credentials/providers/ImCredentials.java | 15 + .../providers/KubernetesCredentials.java | 16 ++ .../providers/OcciCredentials.java | 15 + .../providers/OpenNebulaCredentials.java | 16 ++ .../providers/OpenStackCredentials.java | 56 ++++ .../providers/OpenstackAuthVersion.java | 32 +++ .../providers/VmrcCredentials.java | 15 + .../AuthorizationHeaderTest.java | 162 +++++++---- 52 files changed, 905 insertions(+), 1125 deletions(-) delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java delete mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java rename src/main/java/es/upv/i3m/grycap/im/auth/{ => credentials}/AuthorizationHeader.java (68%) create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/Credentials.java rename src/main/java/es/upv/i3m/grycap/im/auth/{credential => credentials}/ServiceProvider.java (89%) create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/AuthTokenProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseProperties.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseUrlProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/GenericProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/HostProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/OpenStackAuthVersionProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PasswordProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PrivateKeyProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProjectProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProxyProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PublicKeyProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceNameProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceRegionProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TenantProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TokenProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/UsernameProperty.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AmazonEc2Credentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AzureCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DockerCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DummyCredential.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/FogBowCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GceCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GenericCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/ImCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/KubernetesCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OcciCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenNebulaCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenStackCredentials.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenstackAuthVersion.java create mode 100644 src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/VmrcCredentials.java rename src/test/java/es/upv/i3m/grycap/im/auth/{ => credentials}/AuthorizationHeaderTest.java (50%) diff --git a/README.md b/README.md index 2fae292..edc14f9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The javadoc for this project can be found here: [Javadoc](http://indigo-dc.githu 1.1 REQUISITES -------------- This project has been created with maven. -To compile it you will need at least **Apache Maven 3.0.5** and **Java 1.7**. +To compile it you will need at least **Apache Maven 3.0.5** and **Java 1.8**. Maven will take care of downloading all the extra dependencies needed for the project. 1.2 INSTALLING @@ -57,3 +57,17 @@ InfrastructureUri newInfrastructureUri = im.createInfrastructure(readFile(TOSCA_ im.destroyInfrastructure(getInfrastructureId()); ``` The 'im' client always returns a POJO with the information of the call. If an error occurs, an **ImClientErrorException** is thrown. This exception contains the error message and the error code returned by the server. + +### 1.4.3 Create authorization headers +Since version 0.4.5 the infrastructure manager allows to use the **AuthorizationHeader** class and different builders to create the authorization headers. +The usage is as follows: +``` +AuthorizationHeader ah = new AuthorizationHeader(); +Credentials imCred = ImCredentials.buildCredentials().withUsername("user").withPassword("pass"); +Credentials vmrcCred = VmrcCredentials.buildCredentials().withUsername("user").withPassword("pass").withHost("host"); +Credentials dummyCred = DummyCredential.buildCredentials(); +ah.addCredential(imCred); +ah.addCredential(vmrcCred); +ah.addCredential(dummyCred); +InfrastructureManager im = new InfrastructureManager("IM_ENDPOINT", ah); +``` diff --git a/pom.xml b/pom.xml index c3462c2..31f5cd6 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 es.upv.i3m.grycap im-java-api - 0.4.5 + 0.4.6 IM Java API Java client for the REST API of the IM @@ -16,7 +16,7 @@ 3.4 4.12 1.2.17 - 7 + 8 2.10.3 2.17 3.10.4 diff --git a/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java b/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java index 089372a..fbcb26f 100644 --- a/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java +++ b/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java @@ -16,7 +16,7 @@ package es.upv.i3m.grycap.im; -import es.upv.i3m.grycap.im.auth.AuthorizationHeader; +import es.upv.i3m.grycap.im.auth.credentials.AuthorizationHeader; import es.upv.i3m.grycap.im.exceptions.ImClientException; import es.upv.i3m.grycap.im.exceptions.ToscaContentTypeNotSupportedException; import es.upv.i3m.grycap.im.lang.ImMessages; @@ -39,7 +39,8 @@ import java.util.List; /** - * This class offers the user an API to communicate with the Infrastructure Manager. + * This class offers the user an API to communicate with the Infrastructure + * Manager. */ public class InfrastructureManager { @@ -75,8 +76,8 @@ public InfrastructureManager(final String targetUrl, final Path authFile) * @param authorizationHeader * : string with the authorization content */ - public InfrastructureManager(final String targetUrl, final String authorizationHeader) - throws ImClientException { + public InfrastructureManager(final String targetUrl, + final String authorizationHeader) throws ImClientException { imClient = new ImClient(targetUrl, authorizationHeader); } @@ -99,8 +100,8 @@ private ImClient getImClient() { } /** - * Create and configure an infrastructure with the requirements specified in the document of the - * body contents.
+ * Create and configure an infrastructure with the requirements specified in + * the document of the body contents.
* If success, it is returned the URI of the new infrastructure. * * @param infrastructureDefinition @@ -117,27 +118,29 @@ public InfrastructureUri createInfrastructure(String infrastructureDefinition, } /** - * Return a list of URIs referencing the infrastructures associated to the IM user. + * Return a list of URIs referencing the infrastructures associated to the IM + * user. */ public InfrastructureUris getInfrastructureList() throws ImClientException { return getImClient().get(PATH_INFRASTRUCTURES, InfrastructureUris.class); } /** - * Return a list of URIs referencing the virtual machines associated to the infrastructure with ID - * 'infId'. + * Return a list of URIs referencing the virtual machines associated to the + * infrastructure with ID 'infId'. * * @param infId * : infrastructure id */ - public InfrastructureUris getInfrastructureInfo(String infId) throws ImClientException { + public InfrastructureUris getInfrastructureInfo(String infId) + throws ImClientException { return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, InfrastructureUris.class); } /** - * Return information about the virtual machine with ID vmId associated to the infrastructure with - * ID infId. + * Return information about the virtual machine with ID vmId associated to the + * infrastructure with ID infId. * * @param infId * : infrastructure id @@ -145,14 +148,16 @@ public InfrastructureUris getInfrastructureInfo(String infId) throws ImClientExc * : virtual machine id * @return : POJO with the vm info. */ - public VirtualMachineInfo getVmInfo(String infId, String vmId) throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + PATH_VMS + PATH_SEPARATOR + vmId, VirtualMachineInfo.class); + public VirtualMachineInfo getVmInfo(String infId, String vmId) + throws ImClientException { + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + + PATH_SEPARATOR + PATH_VMS + PATH_SEPARATOR + vmId, + VirtualMachineInfo.class); } /** - * Return property 'propertyName' from to the virtual machine with ID 'vmId' associated to the - * infrastructure with ID 'infId'. + * Return property 'propertyName' from to the virtual machine with ID 'vmId' + * associated to the infrastructure with ID 'infId'. * * @param infId * : infrastructure id @@ -161,71 +166,75 @@ public VirtualMachineInfo getVmInfo(String infId, String vmId) throws ImClientEx * @param vmProperty * : VM property to retrieve from the virtual machine */ - public Property getVmProperty(String infId, String vmId, VmProperties vmProperty) - throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + vmProperty.toString(), - Property.class); + public Property getVmProperty(String infId, String vmId, + VmProperties vmProperty) throws ImClientException { + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + + PATH_SEPARATOR + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + + vmProperty.toString(), Property.class); } /** - * Return the contextualization log associated to the infrastructure with ID 'infId'. + * Return the contextualization log associated to the infrastructure with ID + * 'infId'. * * @param infId * : infrastructure id */ - public Property getInfrastructureContMsg(String infId) throws ImClientException { - return getImClient().get( - PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.CONTMSG, - Property.class); + public Property getInfrastructureContMsg(String infId) + throws ImClientException { + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + + PATH_SEPARATOR + ImValues.CONTMSG, Property.class); } /** - * Return a json with the original RADL specified to create the infrastructure with ID 'infId'. + * Return a json with the original RADL specified to create the infrastructure + * with ID 'infId'. * * @param infId * : infrastructure id */ public Property getInfrastructureRadl(String infId) throws ImClientException { - return getImClient().get( - PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RADL, - Property.class); + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + + PATH_SEPARATOR + ImValues.RADL, Property.class); } /** * Return a JSON with the infrastructure and virtual machines states.
- * The JSON has a 'vm_states' array describing the states of all the machines of the - * infrastructure and a 'state' that describes the general state of the infrastructure ( e.g. - * {"vm_states": {"144838424585": "running"}, "state": "running"} ). + * The JSON has a 'vm_states' array describing the states of all the machines + * of the infrastructure and a 'state' that describes the general state of the + * infrastructure ( e.g. {"vm_states": {"144838424585": "running"}, "state": + * "running"} ). * * @param infId * : infrastructure id * @return : json with the infrastructure state */ - public InfrastructureState getInfrastructureState(String infId) throws ImClientException { - return getImClient().get( - PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.STATE, - InfrastructureState.class); + public InfrastructureState getInfrastructureState(String infId) + throws ImClientException { + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + + PATH_SEPARATOR + ImValues.STATE, InfrastructureState.class); } /** - * Undeploy the virtual machines associated to the infrastructure with ID 'infId'. + * Undeploy the virtual machines associated to the infrastructure with ID + * 'infId'. * * @param infId * : infrastructure id */ public void destroyInfrastructure(String infId) throws ImClientException { - getImClient().delete(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, String.class); + getImClient().delete(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, + String.class); } /** - * Add the resources specified in the body contents to the infrastructure with ID 'infId'. The - * RADL restrictions are the same as in - * RPC-XML - * AddResource
+ * Add the resources specified in the body contents to the infrastructure with + * ID 'infId'. The RADL restrictions are the same as in + * + * RPC-XML AddResource
* If success, it is returned a list of URIs of the new virtual machines.
- * The context parameter is optional and is a flag to specify if the contextualization step will - * be launched just after the VM addition.
+ * The context parameter is optional and is a flag to specify if the + * contextualization step will be launched just after the VM addition.
* If not specified the contextualization flag is set to True. * * @param infId @@ -235,28 +244,31 @@ public void destroyInfrastructure(String infId) throws ImClientException { * @param bodyContentType * : set the body content type. Can be RADL, RADL_JSON or TOSCA. * @param context - * : flag to specify if the contextualization step will be launched just after the VM - * addition + * : flag to specify if the contextualization step will be launched + * just after the VM addition * @return : list of URIs of the new virtual machines */ public InfrastructureUris addResource(String infId, String radlFile, - BodyContentType bodyContentType, boolean... context) throws ImClientException { + BodyContentType bodyContentType, boolean... context) + throws ImClientException { RestParameter restParameter = createCallParameters(context); - return getImClient().post(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, radlFile, - bodyContentType.getValue(), InfrastructureUris.class, restParameter); + return getImClient().post(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, + radlFile, bodyContentType.getValue(), InfrastructureUris.class, + restParameter); } private static RestParameter createCallParameters(boolean... context) { return (context != null && context.length > 0) - ? new Parameter(REST_PARAMETER_NAME_CONTEXT, context[0]) : new NoParameter(); + ? new Parameter(REST_PARAMETER_NAME_CONTEXT, context[0]) + : new NoParameter(); } /** - * Undeploy the virtual machine with ID 'vmId' associated to the infrastructure with ID 'infId'. - *
- * The context parameter is optional and is a flag to specify if the contextualization step will - * be launched just after the VM addition.
+ * Undeploy the virtual machine with ID 'vmId' associated to the + * infrastructure with ID 'infId'.
+ * The context parameter is optional and is a flag to specify if the + * contextualization step will be launched just after the VM addition.
* As default the contextualization flag is set to True. * * @param infId @@ -264,21 +276,22 @@ private static RestParameter createCallParameters(boolean... context) { * @param vmIds * : list of virtual machine ids * @param context - * : flag to specify if the contextualization step will be launched just after the VM - * addition + * : flag to specify if the contextualization step will be launched + * just after the VM addition * @throws ImClientException * : exception in the IM client */ - public void removeResource(String infId, List vmIds, boolean... context) - throws ImClientException { + public void removeResource(String infId, List vmIds, + boolean... context) throws ImClientException { String ids = StringUtils.join(vmIds, ","); removeResource(infId, ids, context); } /** - * Undeploy the virtual machine with ID vmId associated to the infrastructure with ID 'infId'.
- * The context parameter is optional and is a flag to specify if the contextualization step will - * be launched just after the VM addition.
+ * Undeploy the virtual machine with ID vmId associated to the infrastructure + * with ID 'infId'.
+ * The context parameter is optional and is a flag to specify if the + * contextualization step will be launched just after the VM addition.
* As default the contextualization flag is set to True. * * @param infId @@ -286,46 +299,49 @@ public void removeResource(String infId, List vmIds, boolean... context) * @param vmId * : virtual machine id * @param context - * : flag to specify if the contextualization step will be launched just after the VM - * addition + * : flag to specify if the contextualization step will be launched + * just after the VM addition */ public void removeResource(String infId, String vmId, boolean... context) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS - + PATH_SEPARATOR + vmId; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + PATH_VMS + PATH_SEPARATOR + vmId; RestParameter restParameter = createCallParameters(context); getImClient().delete(path, String.class, restParameter); } /** - * Stop (but do not undeploy) all the virtual machines associated to the infrastructure with ID - * 'infId'. They can be resumed by the 'startInfrastructure' method. + * Stop (but do not undeploy) all the virtual machines associated to the + * infrastructure with ID 'infId'. They can be resumed by the + * 'startInfrastructure' method. * * @param infId * : infrastructure id */ public void stopInfrastructure(String infId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.STOP; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + ImValues.STOP; getImClient().put(path, String.class); } /** - * Resume all the virtual machines associated to the infrastructure with ID 'infId', previously - * stopped with the 'stopInfrastructure' method. + * Resume all the virtual machines associated to the infrastructure with ID + * 'infId', previously stopped with the 'stopInfrastructure' method. * * @param infId * : infrastructure id */ public void startInfrastructure(String infId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.START; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + ImValues.START; getImClient().put(path, String.class); } /** - * Perform the 'stop' action in the virtual machine with ID 'vmId' associated to the - * infrastructure with ID 'infId'. + * Perform the 'stop' action in the virtual machine with ID 'vmId' associated + * to the infrastructure with ID 'infId'. * * @param infId * : infrastructure id @@ -334,14 +350,14 @@ public void startInfrastructure(String infId) throws ImClientException { */ public void stopVm(String infId, String vmId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS - + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.STOP; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.STOP; getImClient().put(path, String.class); } /** - * Perform the 'start' action in the virtual machine with ID 'vmId' associated to the - * infrastructure with ID 'infId'. + * Perform the 'start' action in the virtual machine with ID 'vmId' associated + * to the infrastructure with ID 'infId'. * * @param infId * : infrastructure id @@ -349,16 +365,18 @@ public void stopVm(String infId, String vmId) throws ImClientException { * : virtual machine id */ public void startVm(String infId, String vmId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS - + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.START; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.START; getImClient().put(path, String.class); } /** - * Change the features of the virtual machine with ID 'vmId' in the infrastructure with with ID - * 'infId', specified by the RADL document specified in the body contents.
+ * Change the features of the virtual machine with ID 'vmId' in the + * infrastructure with with ID 'infId', specified by the RADL document + * specified in the body contents.
* Return a RADL with information about the virtual machine, like - * GetVMInfo. + * + * GetVMInfo. * * @param infId * : infrastructure id @@ -375,9 +393,10 @@ public VirtualMachineInfo alterVm(String infId, String vmId, String radlFile, BodyContentType bodyContentType) throws ImClientException { // The content type must not be TOSCA failIfToscaContentType(bodyContentType); - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS - + PATH_SEPARATOR + vmId; - return getImClient().put(path, radlFile, bodyContentType.getValue(), VirtualMachineInfo.class); + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + PATH_VMS + PATH_SEPARATOR + vmId; + return getImClient().put(path, radlFile, bodyContentType.getValue(), + VirtualMachineInfo.class); } /** @@ -386,34 +405,38 @@ public VirtualMachineInfo alterVm(String infId, String vmId, String radlFile, * @param bodyContentType * : type of the content sent in the body of the message */ - private void failIfToscaContentType(BodyContentType bodyContentType) throws ImClientException { + private void failIfToscaContentType(BodyContentType bodyContentType) + throws ImClientException { if (bodyContentType.equals(BodyContentType.TOSCA)) { - ImJavaApiLogger.severe(InfrastructureManager.class, ImMessages.EXCEPTION_TOSCA_NOT_SUPPORTED); + ImJavaApiLogger.severe(InfrastructureManager.class, + ImMessages.EXCEPTION_TOSCA_NOT_SUPPORTED); throw new ToscaContentTypeNotSupportedException(); } } /** - * Perform the reconfigure action in all the virtual machines in the the infrastructure with ID - * 'infID'.
+ * Perform the reconfigure action in all the virtual machines in the the + * infrastructure with ID 'infID'.
* This method starts the contextualization process again.
- * To reconfigure the VMs see the reconfigure methods that include the 'radl' parameter. + * To reconfigure the VMs see the reconfigure methods that include the 'radl' + * parameter. * * @param infId * : infrastructure id */ public void reconfigure(String infId) throws ImClientException { - String path = - PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RECONFIGURE; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + ImValues.RECONFIGURE; getImClient().put(path, String.class); } /** - * Perform the reconfigure action in all the virtual machines of the infrastructure with ID - * 'infID.' It updates the configuration of the infrastructure as indicated in the 'radlFile'. The - * RADL restrictions are the same as in - * RPC-XML - * Reconfigure
+ * Perform the reconfigure action in all the virtual machines of the + * infrastructure with ID 'infID.' It updates the configuration of the + * infrastructure as indicated in the 'radlFile'. The RADL restrictions are + * the same as in + * + * RPC-XML Reconfigure
* If no RADL is specified, the contextualization process is started again. * * @param infId @@ -423,21 +446,22 @@ public void reconfigure(String infId) throws ImClientException { * @param bodyContentType * : set the body content type. Can be RADL or RADL_JSON */ - public void reconfigure(String infId, String radlFile, BodyContentType bodyContentType) - throws ImClientException { + public void reconfigure(String infId, String radlFile, + BodyContentType bodyContentType) throws ImClientException { // The content type must not be TOSCA failIfToscaContentType(bodyContentType); - String path = - PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RECONFIGURE; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + ImValues.RECONFIGURE; getImClient().put(path, radlFile, bodyContentType.getValue(), String.class); } /** - * Perform the reconfigure action in all the virtual machines of the infrastructure with ID - * 'infID.' It updates the configuration of the infrastructure as indicated in the 'radlFile'. The - * RADL restrictions are the same as in - * RPC-XML - * Reconfigure
+ * Perform the reconfigure action in all the virtual machines of the + * infrastructure with ID 'infID.' It updates the configuration of the + * infrastructure as indicated in the 'radlFile'. The RADL restrictions are + * the same as in + * + * RPC-XML Reconfigure
* If no RADL is specified, the contextualization process is started again. * * @param infId @@ -447,17 +471,20 @@ public void reconfigure(String infId, String radlFile, BodyContentType bodyConte * @param bodyContentType * : set the body content type. Can be RADL or RADL_JSON * @param vmList - * : comma separated list of IDs of the VMs to reconfigure. If not specified all the VMs - * will be reconfigured. + * : comma separated list of IDs of the VMs to reconfigure. If not + * specified all the VMs will be reconfigured. */ - public void reconfigure(String infId, String radlFile, BodyContentType bodyContentType, - List vmList) throws ImClientException { + public void reconfigure(String infId, String radlFile, + BodyContentType bodyContentType, List vmList) + throws ImClientException { // The content type must not be TOSCA failIfToscaContentType(bodyContentType); - RestParameter parameters = new Parameter(REST_PARAMETER_NAME_VMLIST, vmList); - String path = - PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RECONFIGURE; - getImClient().put(path, radlFile, bodyContentType.getValue(), String.class, parameters); + RestParameter parameters = + new Parameter(REST_PARAMETER_NAME_VMLIST, vmList); + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + ImValues.RECONFIGURE; + getImClient().put(path, radlFile, bodyContentType.getValue(), String.class, + parameters); } /** @@ -465,9 +492,11 @@ public void reconfigure(String infId, String radlFile, BodyContentType bodyConte * * @param infId * : infrastructure id - * @return : InfrastructureStatus class with an internal map containing the outputs + * @return : InfrastructureStatus class with an internal map containing the + * outputs */ - public InfOutputValues getInfrastructureOutputs(String infId) throws ImClientException { + public InfOutputValues getInfrastructureOutputs(String infId) + throws ImClientException { String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + REST_PARAMETER_INFRASTRUCTURE_OUTPUTS; return getImClient().get(path, InfOutputValues.class); diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java deleted file mode 100644 index 8c12dbe..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java +++ /dev/null @@ -1,38 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -public abstract class AbstractCredential> - implements Credential { - - protected > AbstractCredential( - B builder) { - } - - @Override - public String serialize() { - return serialize(null).toString(); - } - - protected StringBuilder serialize(final StringBuilder sb) { - StringBuilder serializedSb = sb; - if (serializedSb == null) { - serializedSb = new StringBuilder(); - } - serializedSb.append("id = ").append(getServiceProvider().getId()) - .append(" ; "); - serializedSb.append("type = ").append(getServiceProvider().getType()); - return serializedSb; - } - - public static boolean isNullOrEmpty(String string) { - return (string == null) || string.isEmpty(); - } - - //@formatter:off - public abstract static class AbstractCredentialBuilder - , T extends AbstractCredential> - implements CredentialBuilder { - //@formatter:on - - } - -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java deleted file mode 100644 index ad714ab..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java +++ /dev/null @@ -1,50 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -public abstract class AbstractTokenCredential> - extends AbstractCredential { - - protected String token; - - protected > AbstractTokenCredential( - B builder) { - super(builder); - setToken(builder.getToken()); - } - - public String getToken() { - return token; - } - - private void setToken(String token) { - if (isNullOrEmpty(token)) { - throw new IllegalArgumentException("Token must not be blank"); - } - this.token = token; - } - - @Override - protected StringBuilder serialize(final StringBuilder sb) { - StringBuilder serializedSb = sb; - serializedSb = super.serialize(serializedSb); - serializedSb.append(" ; token = ").append(token); - return serializedSb; - } - - //@formatter:off - public abstract static class AbstractTokenCredentialBuilder - , T extends AbstractTokenCredential> - extends AbstractCredentialBuilder { - //@formatter:on - private String token; - - @SuppressWarnings("unchecked") - public B withToken(String token) { - this.token = token; - return (B) this; - } - - public String getToken() { - return token; - } - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java deleted file mode 100644 index b5c94a4..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java +++ /dev/null @@ -1,82 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -//@formatter:off -public abstract class AbstractUsernamePasswordCredential> - extends AbstractCredential { - //@formatter:on - - private String username; - private String password; - - //@formatter:off - protected > - AbstractUsernamePasswordCredential(B builder) { - //@formatter:on - - super(builder); - username = builder.getUsername(); - password = builder.getPassword(); - } - - public String getUsername() { - return username; - } - - public void setUsername(final String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(final String password) { - this.password = password; - } - - @Override - public StringBuilder serialize(final StringBuilder sb) { - StringBuilder serializedSb = sb; - serializedSb = super.serialize(serializedSb); - if (!isNullOrEmpty(username)) { - serializedSb.append(" ; username = ").append(username); - } - if (!isNullOrEmpty(password)) { - serializedSb.append(" ; password = ").append(password); - } - return serializedSb; - } - - //@formatter:off - public abstract static class AbstractUsernamePasswordCredentialBuilder - , - T extends AbstractUsernamePasswordCredential> - extends AbstractCredentialBuilder { - //@formatter:on - - private String username; - private String password; - - @SuppressWarnings("unchecked") - public B withUsername(final String username) { - this.username = username; - return (B) this; - } - - @SuppressWarnings("unchecked") - public B withPassword(final String password) { - this.password = password; - return (B) this; - } - - public String getUsername() { - return username; - } - - public String getPassword() { - return password; - } - - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java deleted file mode 100644 index 164d621..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java +++ /dev/null @@ -1,14 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -public interface Credential> { - - public ServiceProvider getServiceProvider(); - - public String serialize(); - - public interface CredentialBuilder { - - public T build(); - - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java deleted file mode 100644 index 472b9ac..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/docker/DockerCredential.java +++ /dev/null @@ -1,62 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.docker; - -import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class DockerCredential extends AbstractCredential { - - private String host; - - protected DockerCredential(DockerCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - } - - public String getHost() { - return host; - } - - private void setHost(final String host) { - if (isNullOrEmpty(host)) { - throw new IllegalArgumentException("Host must not be blank"); - } - this.host = host; - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.DOCKER; - } - - @Override - public StringBuilder serialize(final StringBuilder sb) { - StringBuilder serializedSb = sb; - serializedSb = super.serialize(serializedSb); - serializedSb.append(" ; host = ").append(host); - return serializedSb; - } - - public static DockerCredentialBuilder getBuilder() { - return new DockerCredentialBuilder(); - } - - public static class DockerCredentialBuilder extends - AbstractCredentialBuilder { - - private String host; - - public String getHost() { - return host; - } - - public DockerCredentialBuilder withHost(final String host) { - this.host = host; - return this; - } - - @Override - public DockerCredential build() { - return new DockerCredential(this); - } - } -} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java deleted file mode 100644 index f62b2c8..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/dummy/DummyCredential.java +++ /dev/null @@ -1,31 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.dummy; - -import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class DummyCredential extends AbstractCredential { - - protected DummyCredential(DummyCredentialBuilder builder) { - super(builder); - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.DUMMY; - } - - public static DummyCredentialBuilder getBuilder() { - return new DummyCredentialBuilder(); - } - - public static class DummyCredentialBuilder extends - AbstractCredentialBuilder { - - @Override - public DummyCredential build() { - return new DummyCredential(this); - } - - } - -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java deleted file mode 100644 index ef27f61..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ec2/AmazonEc2UserPwdCredential.java +++ /dev/null @@ -1,34 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.ec2; - -import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class AmazonEc2UserPwdCredential - extends AbstractUsernamePasswordCredential { - - protected AmazonEc2UserPwdCredential( - AmazonEc2UserPwdCredentialBuilder builder) { - super(builder); - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.EC2; - } - - public static AmazonEc2UserPwdCredentialBuilder getBuilder() { - return new AmazonEc2UserPwdCredentialBuilder(); - } - - //@formatter:off - public static class AmazonEc2UserPwdCredentialBuilder extends - AbstractUsernamePasswordCredentialBuilder { - //@formatter:on - - @Override - public AmazonEc2UserPwdCredential build() { - return new AmazonEc2UserPwdCredential(this); - } - } -} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java deleted file mode 100644 index 5a57bf3..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/gce/GceUserPwdCredential.java +++ /dev/null @@ -1,65 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.gce; - -import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class GceUserPwdCredential - extends AbstractUsernamePasswordCredential { - - private String project; - - protected GceUserPwdCredential(GceUserPwdCredentialBuilder builder) { - super(builder); - setProject(builder.getProject()); - } - - public String getProject() { - return project; - } - - private void setProject(String project) { - if (isNullOrEmpty(project)) { - throw new IllegalArgumentException("Project must not be blank"); - } - this.project = project; - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.GCE; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; project = ").append(project); - return sb; - } - - public static GceUserPwdCredentialBuilder getBuilder() { - return new GceUserPwdCredentialBuilder(); - } - - //@formatter:off - public static class GceUserPwdCredentialBuilder extends - AbstractUsernamePasswordCredentialBuilder { - //@formatter:on - - private String project; - - public String getProject() { - return project; - } - - public GceUserPwdCredentialBuilder withProject(String project) { - this.project = project; - return this; - } - - @Override - public GceUserPwdCredential build() { - return new GceUserPwdCredential(this); - } - } -} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java deleted file mode 100644 index 29e3ffe..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java +++ /dev/null @@ -1,65 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.im; - -import es.upv.i3m.grycap.im.auth.credential.AbstractTokenCredential; -import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public final class ImCredential { - - public static class ImTokenCredential - extends AbstractTokenCredential { - - protected ImTokenCredential(ImTokenCredentialBuilder builder) { - super(builder); - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.INFRASTRUCTURE_MANAGER; - } - - public static ImTokenCredentialBuilder getBuilder() { - return new ImTokenCredentialBuilder(); - } - - public static class ImTokenCredentialBuilder extends - AbstractTokenCredentialBuilder { - - @Override - public ImTokenCredential build() { - return new ImTokenCredential(this); - } - - } - } - - public static class ImUsernamePasswordCredential - extends AbstractUsernamePasswordCredential { - - protected ImUsernamePasswordCredential( - ImUsernamePasswordCredentialBuilder builder) { - super(builder); - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.INFRASTRUCTURE_MANAGER; - } - - public static ImUsernamePasswordCredentialBuilder getBuilder() { - return new ImUsernamePasswordCredentialBuilder(); - } - - //@formatter:off - public static class ImUsernamePasswordCredentialBuilder extends - AbstractUsernamePasswordCredentialBuilder { - //@formatter:on - - @Override - public ImUsernamePasswordCredential build() { - return new ImUsernamePasswordCredential(this); - } - } - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java deleted file mode 100644 index 18a878c..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java +++ /dev/null @@ -1,85 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.occi; - -import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class OcciCredential extends AbstractCredential { - - private String host; - private String proxy; - - protected OcciCredential(OcciCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - setProxy(builder.getProxy()); - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.OCCI; - } - - public String getHost() { - return host; - } - - private void setHost(String host) { - if (isNullOrEmpty(host)) { - throw new IllegalArgumentException("host must not be blank"); - } - this.host = host; - } - - public String getProxy() { - return proxy; - } - - private void setProxy(String proxy) { - if (isNullOrEmpty(proxy)) { - throw new IllegalArgumentException("proxy must not be blank"); - } - this.proxy = proxy; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - sb.append(" ; proxy = ").append(proxy.replaceAll("\r?\n", "\\n")); - return sb; - } - - public static OcciCredentialBuilder getBuilder() { - return new OcciCredentialBuilder(); - } - - public static class OcciCredentialBuilder - extends AbstractCredentialBuilder { - - private String host; - private String proxy; - - public OcciCredentialBuilder withHost(String host) { - this.host = host; - return this; - } - - public OcciCredentialBuilder withProxy(String proxy) { - this.proxy = proxy; - return this; - } - - public String getHost() { - return host; - } - - public String getProxy() { - return proxy; - } - - @Override - public OcciCredential build() { - return new OcciCredential(this); - } - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java deleted file mode 100644 index 2a960f1..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java +++ /dev/null @@ -1,63 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.opennebula; - -import es.upv.i3m.grycap.im.auth.credential.AbstractTokenCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class OpenNebulaTokenCredential - extends AbstractTokenCredential { - - private String host; - - protected OpenNebulaTokenCredential( - OpennebulaTokenCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - } - - public String getHost() { - return host; - } - - private void setHost(String host) { - if (isNullOrEmpty(host)) { - throw new IllegalArgumentException("Host must not be blank"); - } - this.host = host; - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.OPENNEBULA; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - return sb; - } - - public static OpennebulaTokenCredentialBuilder getBuilder() { - return new OpennebulaTokenCredentialBuilder(); - } - - public static class OpennebulaTokenCredentialBuilder extends - AbstractTokenCredentialBuilder { - - private String host; - - public String getHost() { - return host; - } - - public OpennebulaTokenCredentialBuilder withHost(String host) { - this.host = host; - return this; - } - - @Override - public OpenNebulaTokenCredential build() { - return new OpenNebulaTokenCredential(this); - } - } -} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java deleted file mode 100644 index 787e4db..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java +++ /dev/null @@ -1,66 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.opennebula; - -import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class OpenNebulaUserPwdCredential - extends AbstractUsernamePasswordCredential { - - private String host; - - protected OpenNebulaUserPwdCredential( - OpennebulaUserPwdCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - } - - public String getHost() { - return host; - } - - private void setHost(String host) { - if (isNullOrEmpty(host)) { - throw new IllegalArgumentException("host must not be blank"); - } - this.host = host; - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.OPENNEBULA; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - return sb; - } - - public static OpennebulaUserPwdCredentialBuilder getBuilder() { - return new OpennebulaUserPwdCredentialBuilder(); - } - - //@formatter:off - public static class OpennebulaUserPwdCredentialBuilder extends - AbstractUsernamePasswordCredentialBuilder { - //@formatter:on - - private String host; - - public String getHost() { - return host; - } - - public OpennebulaUserPwdCredentialBuilder withHost(String host) { - this.host = host; - return this; - } - - @Override - public OpenNebulaUserPwdCredential build() { - return new OpenNebulaUserPwdCredential(this); - } - } -} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java deleted file mode 100644 index e63cb7d..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java +++ /dev/null @@ -1,19 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.openstack; - -public enum OpenstackAuthVersion { - - //@formatter:off - PASSWORD_2_0("2.0_password"), - PASSWORD_3_X("3.X_password"); - //@formatter:on - - private final String value; - - OpenstackAuthVersion(String value) { - this.value = value; - } - - public String getValue() { - return value; - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java deleted file mode 100644 index 10ff82b..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java +++ /dev/null @@ -1,198 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.openstack; - -import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class OpenstackCredential - extends AbstractUsernamePasswordCredential { - - private OpenstackAuthVersion authVersion = OpenstackAuthVersion.PASSWORD_2_0; - private String tenant; - private String host; - private String baseUrl; - private String serviceRegion; - private String serviceName; - private String authToken; - - protected OpenstackCredential(OpenstackCredentialBuilder builder) { - super(builder); - authVersion = builder.getAuthVersion(); - tenant = builder.getTenant(); - setHost(builder.getHost()); - baseUrl = builder.getBaseUrl(); - serviceRegion = builder.getServiceRegion(); - serviceName = builder.getServiceName(); - authToken = builder.getAuthToken(); - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.OPENSTACK; - } - - public OpenstackAuthVersion getAuthVersion() { - return authVersion; - } - - public void setAuthVersion(OpenstackAuthVersion authVersion) { - this.authVersion = authVersion; - } - - public String getTenant() { - return tenant; - } - - public void setTenant(String tenant) { - this.tenant = tenant; - } - - public String getHost() { - return host; - } - - private void setHost(String host) { - if (isNullOrEmpty(host)) { - throw new IllegalArgumentException("Host must not be blank"); - } - this.host = host; - } - - public String getBaseUrl() { - return baseUrl; - } - - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public String getServiceRegion() { - return serviceRegion; - } - - public void setServiceRegion(String serviceRegion) { - this.serviceRegion = serviceRegion; - } - - public String getServiceName() { - return serviceName; - } - - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - } - - public String getAuthToken() { - return authToken; - } - - public void setAuthToken(String authToken) { - this.authToken = authToken; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - if (authVersion != OpenstackAuthVersion.PASSWORD_2_0) { - sb.append(" ; OpenstackAuthVersion = ").append(authVersion.getValue()); - } - if (!isNullOrEmpty(baseUrl)) { - sb.append(" ; base_url = ").append(baseUrl); - } - if (!isNullOrEmpty(serviceRegion)) { - sb.append(" ; service_region = ").append(serviceRegion); - } - if (!isNullOrEmpty(serviceName)) { - sb.append(" ; service_name = ").append(serviceName); - } - if (!isNullOrEmpty(authToken)) { - sb.append(" ; auth_token = ").append(authToken); - } - return sb; - } - - public static OpenstackCredentialBuilder getBuilder() { - return new OpenstackCredentialBuilder(); - } - - public static class OpenstackCredentialBuilder extends - AbstractUsernamePasswordCredentialBuilder { - - private OpenstackAuthVersion authVersion; - private String tenant; - private String host; - private String baseUrl; - private String serviceRegion; - private String serviceName; - private String authToken; - - public OpenstackAuthVersion getAuthVersion() { - return authVersion; - } - - public OpenstackCredentialBuilder - withAuthVersion(OpenstackAuthVersion authVersion) { - this.authVersion = authVersion; - return this; - } - - public String getTenant() { - return tenant; - } - - public OpenstackCredentialBuilder withTenant(String tenant) { - this.tenant = tenant; - return this; - } - - public String getHost() { - return host; - } - - public OpenstackCredentialBuilder withHost(String host) { - this.host = host; - return this; - } - - public String getBaseUrl() { - return baseUrl; - } - - public OpenstackCredentialBuilder withBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - return this; - } - - public String getServiceRegion() { - return serviceRegion; - } - - public OpenstackCredentialBuilder withServiceRegion(String serviceRegion) { - this.serviceRegion = serviceRegion; - return this; - } - - public String getServiceName() { - return serviceName; - } - - public OpenstackCredentialBuilder withServiceName(String serviceName) { - this.serviceName = serviceName; - return this; - } - - public String getAuthToken() { - return authToken; - } - - public OpenstackCredentialBuilder withAuthToken(String authToken) { - this.authToken = authToken; - return this; - } - - @Override - public OpenstackCredential build() { - return new OpenstackCredential(this); - } - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java deleted file mode 100644 index 3581b08..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java +++ /dev/null @@ -1,64 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential.vmrc; - -import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.ServiceProvider; - -public class VmrcCredential - extends AbstractUsernamePasswordCredential { - - private String host; - - protected VmrcCredential(VmrcCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - - } - - @Override - public ServiceProvider getServiceProvider() { - return ServiceProvider.VMRC; - } - - public String getHost() { - return host; - } - - private void setHost(String host) { - if (isNullOrEmpty(host)) { - throw new IllegalArgumentException("host must not be blank"); - } - this.host = host; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - return sb; - } - - public static VmrcCredentialBuilder getBuilder() { - return new VmrcCredentialBuilder(); - } - - public static class VmrcCredentialBuilder extends - AbstractUsernamePasswordCredentialBuilder { - - private String host; - - public VmrcCredentialBuilder withHost(String host) { - this.host = host; - return this; - } - - public String getHost() { - return host; - } - - @Override - public VmrcCredential build() { - return new VmrcCredential(this); - } - - } -} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeader.java similarity index 68% rename from src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java rename to src/main/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeader.java index c186814..04562aa 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeader.java @@ -1,6 +1,4 @@ -package es.upv.i3m.grycap.im.auth; - -import es.upv.i3m.grycap.im.auth.credential.Credential; +package es.upv.i3m.grycap.im.auth.credentials; import java.util.ArrayList; import java.util.Iterator; @@ -9,23 +7,23 @@ public class AuthorizationHeader { private static final String ERROR_MESSAGE = "Credentials must not be null"; - private List credentials = new ArrayList<>(); + private List credentials = new ArrayList<>(); - public List getCredentials() { + public List getCredentials() { return credentials; } /** * Sets the credentials information. */ - public void setCredentialsAuthInfos(List credentials) { + public void setCredentialsAuthInfos(List credentials) { if (credentials == null) { throw new IllegalArgumentException(ERROR_MESSAGE); } this.credentials = credentials; } - public void addCredential(Credential credential) { + public void addCredential(Credentials credential) { credentials.add(credential); } @@ -34,7 +32,7 @@ public void addCredential(Credential credential) { */ public String serialize() { StringBuilder sb = new StringBuilder(); - Iterator it = credentials.iterator(); + Iterator it = credentials.iterator(); while (it.hasNext()) { String serializedAuthInfo = it.next().serialize(); sb.append(serializedAuthInfo); diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/Credentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/Credentials.java new file mode 100644 index 0000000..fc8a42c --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/Credentials.java @@ -0,0 +1,10 @@ +package es.upv.i3m.grycap.im.auth.credentials; + +public interface Credentials { + + public String serialize(); + + public default boolean isNullOrEmpty(String string) { + return (string == null) || string.isEmpty(); + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/ServiceProvider.java similarity index 89% rename from src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java rename to src/main/java/es/upv/i3m/grycap/im/auth/credentials/ServiceProvider.java index 1bbfe6d..f379283 100644 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceProvider.java +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/ServiceProvider.java @@ -1,4 +1,4 @@ -package es.upv.i3m.grycap.im.auth.credential; +package es.upv.i3m.grycap.im.auth.credentials; public enum ServiceProvider { @@ -8,7 +8,7 @@ public enum ServiceProvider { DUMMY("dummy","Dummy"), OPENNEBULA("one","OpenNebula"), EC2("ec2","EC2"), - FOG_BOW("fogbow","FogBow"), + FOG_BOW("fog","FogBow"), OPENSTACK("ost","OpenStack"), OCCI("occi","OCCI"), DOCKER("docker","Docker"), diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/AuthTokenProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/AuthTokenProperty.java new file mode 100644 index 0000000..24975b7 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/AuthTokenProperty.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class AuthTokenProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "auth_token"; + private static final String ERROR_MESSAGE = + "Authorization token must not be blank"; + + public AuthTokenProperty(Credentials credential, String authToken) { + super(credential, PROPERTY_NAME, authToken, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseProperties.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseProperties.java new file mode 100644 index 0000000..fba4808 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseProperties.java @@ -0,0 +1,27 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class BaseProperties implements Credentials { + + private final String id; + private final String type; + + public BaseProperties(final String id, final String type) { + this.id = id; + this.type = type; + } + + @Override + public String serialize() { + StringBuilder credentials = new StringBuilder(); + if (!isNullOrEmpty(id)) { + credentials.append("id = ").append(id).append(" ; "); + } + if (!isNullOrEmpty(type)) { + credentials.append("type = ").append(type); + } + return credentials.toString(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseUrlProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseUrlProperty.java new file mode 100644 index 0000000..5657487 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/BaseUrlProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class BaseUrlProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "base_url"; + private static final String ERROR_MESSAGE = "Base url must not be blank"; + + public BaseUrlProperty(Credentials credential, String baseUrl) { + super(credential, PROPERTY_NAME, baseUrl, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/GenericProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/GenericProperty.java new file mode 100644 index 0000000..f21169d --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/GenericProperty.java @@ -0,0 +1,42 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class GenericProperty implements Credentials { + + private final Credentials credential; + private String propertyName; + private String propertyValue; + + /** + * Generic constructor for all the properties that the authorization header + * can have. + */ + public GenericProperty(Credentials credential, String propertyName, + String propertyValue, String errorMessage) { + this.credential = credential; + this.propertyName = propertyName; + if (isNullOrEmpty(propertyValue)) { + throw new IllegalArgumentException(errorMessage); + } + this.propertyValue = propertyValue; + } + + protected String getPropertyValue() { + return propertyValue; + } + + protected Credentials getCredentials() { + return credential; + } + + @Override + public String serialize() { + StringBuilder credentials = new StringBuilder(credential.serialize()); + if (!isNullOrEmpty(propertyValue)) { + credentials.append(" ; " + propertyName + " = ").append(propertyValue); + } + return credentials.toString(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/HostProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/HostProperty.java new file mode 100644 index 0000000..2146b66 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/HostProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class HostProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "host"; + private static final String ERROR_MESSAGE = "Host must not be blank"; + + public HostProperty(Credentials credential, String host) { + super(credential, PROPERTY_NAME, host, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/OpenStackAuthVersionProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/OpenStackAuthVersionProperty.java new file mode 100644 index 0000000..792c201 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/OpenStackAuthVersionProperty.java @@ -0,0 +1,27 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.OpenstackAuthVersion; + +public class OpenStackAuthVersionProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "auth_version"; + private static final String ERROR_MESSAGE = + "OpenStack auth version must not be blank"; + + public OpenStackAuthVersionProperty(Credentials credential, + OpenstackAuthVersion openstackAuthVersion) { + super(credential, PROPERTY_NAME, openstackAuthVersion.getValue(), + ERROR_MESSAGE); + } + + @Override + public String serialize() { + // Default property, not needed to add it to the credentials + if (!OpenstackAuthVersion.PASSWORD_2_0.compare(getPropertyValue())) { + return super.serialize(); + } + return getCredentials().serialize(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PasswordProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PasswordProperty.java new file mode 100644 index 0000000..80608df --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PasswordProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class PasswordProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "password"; + private static final String ERROR_MESSAGE = "Password must not be blank"; + + public PasswordProperty(Credentials credential, String password) { + super(credential, PROPERTY_NAME, password, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PrivateKeyProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PrivateKeyProperty.java new file mode 100644 index 0000000..259ded7 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PrivateKeyProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class PrivateKeyProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "public_key"; + private static final String ERROR_MESSAGE = "Public key must not be blank"; + + public PrivateKeyProperty(Credentials credential, String publicKey) { + super(credential, PROPERTY_NAME, publicKey, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProjectProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProjectProperty.java new file mode 100644 index 0000000..4cd7745 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProjectProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class ProjectProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "project"; + private static final String ERROR_MESSAGE = "Project must not be blank"; + + public ProjectProperty(Credentials credential, String project) { + super(credential, PROPERTY_NAME, project, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProxyProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProxyProperty.java new file mode 100644 index 0000000..4a1e809 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ProxyProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class ProxyProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "proxy"; + private static final String ERROR_MESSAGE = "Proxy must not be blank"; + + public ProxyProperty(Credentials credential, String proxy) { + super(credential, PROPERTY_NAME, proxy, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PublicKeyProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PublicKeyProperty.java new file mode 100644 index 0000000..160ebd1 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/PublicKeyProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class PublicKeyProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "private_key"; + private static final String ERROR_MESSAGE = "Private key must not be blank"; + + public PublicKeyProperty(Credentials credential, String privateKey) { + super(credential, PROPERTY_NAME, privateKey, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceNameProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceNameProperty.java new file mode 100644 index 0000000..58e7d94 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceNameProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class ServiceNameProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "service_name"; + private static final String ERROR_MESSAGE = "Service name must not be blank"; + + public ServiceNameProperty(Credentials credential, String serviceName) { + super(credential, PROPERTY_NAME, serviceName, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceRegionProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceRegionProperty.java new file mode 100644 index 0000000..4a0eb0e --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/ServiceRegionProperty.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class ServiceRegionProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "service_region"; + private static final String ERROR_MESSAGE = + "Service region must not be blank"; + + public ServiceRegionProperty(Credentials credential, String serviceRegion) { + super(credential, PROPERTY_NAME, serviceRegion, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TenantProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TenantProperty.java new file mode 100644 index 0000000..6825d03 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TenantProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class TenantProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "tenant"; + private static final String ERROR_MESSAGE = "Tenant must not be blank"; + + public TenantProperty(Credentials credential, String tenant) { + super(credential, PROPERTY_NAME, tenant, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TokenProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TokenProperty.java new file mode 100644 index 0000000..a2b206c --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/TokenProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class TokenProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "token"; + private static final String ERROR_MESSAGE = "Token must not be blank"; + + public TokenProperty(Credentials credential, String token) { + super(credential, PROPERTY_NAME, token, ERROR_MESSAGE); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/UsernameProperty.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/UsernameProperty.java new file mode 100644 index 0000000..e79bced --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/properties/UsernameProperty.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credentials.properties; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; + +public class UsernameProperty extends GenericProperty { + + private static final String PROPERTY_NAME = "username"; + private static final String ERROR_MESSAGE = "Username must not be blank"; + + public UsernameProperty(Credentials credential, String username) { + super(credential, PROPERTY_NAME, username, ERROR_MESSAGE); + } + +} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AmazonEc2Credentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AmazonEc2Credentials.java new file mode 100644 index 0000000..c45caff --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AmazonEc2Credentials.java @@ -0,0 +1,16 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class AmazonEc2Credentials + extends GenericCredentials { + + private AmazonEc2Credentials() { + super(ServiceProvider.EC2); + } + + public static AmazonEc2Credentials buildCredentials() { + return new AmazonEc2Credentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AzureCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AzureCredentials.java new file mode 100644 index 0000000..ed78200 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/AzureCredentials.java @@ -0,0 +1,27 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; +import es.upv.i3m.grycap.im.auth.credentials.properties.PrivateKeyProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.PublicKeyProperty; + +public class AzureCredentials extends GenericCredentials { + + private AzureCredentials() { + super(ServiceProvider.AZURE); + } + + public static AzureCredentials buildCredentials() { + return new AzureCredentials(); + } + + public AzureCredentials withPublicKey(String publicKey) { + setCredentials(new PublicKeyProperty(getCredentials(), publicKey)); + return this; + } + + public AzureCredentials withPrivateKey(String privateKey) { + setCredentials(new PrivateKeyProperty(getCredentials(), privateKey)); + return this; + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DockerCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DockerCredentials.java new file mode 100644 index 0000000..d60971c --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DockerCredentials.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class DockerCredentials extends GenericCredentials { + + private DockerCredentials() { + super(ServiceProvider.DOCKER); + } + + public static DockerCredentials buildCredentials() { + return new DockerCredentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DummyCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DummyCredential.java new file mode 100644 index 0000000..a31c6c1 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/DummyCredential.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class DummyCredential extends GenericCredentials { + + private DummyCredential() { + super(ServiceProvider.DUMMY); + } + + public static DummyCredential buildCredentials() { + return new DummyCredential(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/FogBowCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/FogBowCredentials.java new file mode 100644 index 0000000..9f1c024 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/FogBowCredentials.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class FogBowCredentials extends GenericCredentials { + + private FogBowCredentials() { + super(ServiceProvider.FOG_BOW); + } + + public static FogBowCredentials buildCredentials() { + return new FogBowCredentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GceCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GceCredentials.java new file mode 100644 index 0000000..a58ab92 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GceCredentials.java @@ -0,0 +1,21 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; +import es.upv.i3m.grycap.im.auth.credentials.properties.ProjectProperty; + +public class GceCredentials extends GenericCredentials { + + private GceCredentials() { + super(ServiceProvider.GCE); + } + + public static GceCredentials buildCredentials() { + return new GceCredentials(); + } + + public GceCredentials withProject(String project) { + setCredentials(new ProjectProperty(getCredentials(), project)); + return this; + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GenericCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GenericCredentials.java new file mode 100644 index 0000000..434af7f --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/GenericCredentials.java @@ -0,0 +1,65 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.Credentials; +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; +import es.upv.i3m.grycap.im.auth.credentials.properties.BaseProperties; +import es.upv.i3m.grycap.im.auth.credentials.properties.HostProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.PasswordProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.ProxyProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.TokenProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.UsernameProperty; + +public abstract class GenericCredentials implements Credentials { + + protected Credentials credentials; + + protected GenericCredentials(ServiceProvider provider) { + credentials = new BaseProperties(provider.getId(), provider.getType()); + } + + @Override + public String serialize() { + return credentials.serialize(); + } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } + + // Below the most common methods used in all the credentials + + @SuppressWarnings("unchecked") + public T withUsername(String username) { + setCredentials(new UsernameProperty(getCredentials(), username)); + return (T) this; + } + + @SuppressWarnings("unchecked") + public T withPassword(String password) { + setCredentials(new PasswordProperty(getCredentials(), password)); + return (T) this; + } + + @SuppressWarnings("unchecked") + public T withHost(String host) { + setCredentials(new HostProperty(getCredentials(), host)); + return (T) this; + } + + @SuppressWarnings("unchecked") + public T withToken(String token) { + setCredentials(new TokenProperty(getCredentials(), token)); + return (T) this; + } + + @SuppressWarnings("unchecked") + public T withProxy(String proxy) { + setCredentials(new ProxyProperty(getCredentials(), proxy)); + return (T) this; + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/ImCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/ImCredentials.java new file mode 100644 index 0000000..e343cc9 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/ImCredentials.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class ImCredentials extends GenericCredentials { + + private ImCredentials() { + super(ServiceProvider.INFRASTRUCTURE_MANAGER); + } + + public static ImCredentials buildCredentials() { + return new ImCredentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/KubernetesCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/KubernetesCredentials.java new file mode 100644 index 0000000..58e2068 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/KubernetesCredentials.java @@ -0,0 +1,16 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class KubernetesCredentials + extends GenericCredentials { + + private KubernetesCredentials() { + super(ServiceProvider.KUBERNETES); + } + + public static KubernetesCredentials buildCredentials() { + return new KubernetesCredentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OcciCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OcciCredentials.java new file mode 100644 index 0000000..20d4563 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OcciCredentials.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class OcciCredentials extends GenericCredentials { + + private OcciCredentials() { + super(ServiceProvider.OCCI); + } + + public static OcciCredentials buildCredentials() { + return new OcciCredentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenNebulaCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenNebulaCredentials.java new file mode 100644 index 0000000..4d6e8a2 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenNebulaCredentials.java @@ -0,0 +1,16 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class OpenNebulaCredentials + extends GenericCredentials { + + private OpenNebulaCredentials() { + super(ServiceProvider.OPENNEBULA); + } + + public static OpenNebulaCredentials buildCredentials() { + return new OpenNebulaCredentials(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenStackCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenStackCredentials.java new file mode 100644 index 0000000..4a47269 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenStackCredentials.java @@ -0,0 +1,56 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; +import es.upv.i3m.grycap.im.auth.credentials.properties.AuthTokenProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.BaseUrlProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.OpenStackAuthVersionProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.ServiceNameProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.ServiceRegionProperty; +import es.upv.i3m.grycap.im.auth.credentials.properties.TenantProperty; + +public class OpenStackCredentials + extends GenericCredentials { + + private OpenStackCredentials() { + super(ServiceProvider.OPENSTACK); + } + + public static OpenStackCredentials buildCredentials() { + return new OpenStackCredentials(); + } + + public OpenStackCredentials withTenant(String tenant) { + setCredentials(new TenantProperty(getCredentials(), tenant)); + return this; + } + + public OpenStackCredentials withBaseUrl(String baseUrl) { + setCredentials(new BaseUrlProperty(getCredentials(), baseUrl)); + return this; + } + + public OpenStackCredentials withServiceRegion(String serviceRegion) { + setCredentials(new ServiceRegionProperty(getCredentials(), serviceRegion)); + return this; + } + + public OpenStackCredentials withServiceName(String serviceName) { + setCredentials(new ServiceNameProperty(getCredentials(), serviceName)); + return this; + } + + public OpenStackCredentials withAuthToken(String authToken) { + setCredentials(new AuthTokenProperty(getCredentials(), authToken)); + return this; + } + + /** + * Sets the authorization version specific for OpenStack. + */ + public OpenStackCredentials + withAuthVersion(OpenstackAuthVersion authVersion) { + setCredentials( + new OpenStackAuthVersionProperty(getCredentials(), authVersion)); + return this; + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenstackAuthVersion.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenstackAuthVersion.java new file mode 100644 index 0000000..57f9d60 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/OpenstackAuthVersion.java @@ -0,0 +1,32 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +public enum OpenstackAuthVersion { + + //@formatter:off + PASSWORD_2_0("2.0_password"), + PASSWORD_3_X("3.X_password"), + PASSWORD_3_X_TOKEN("3.x_oidc_access_token"); + //@formatter:on + + private final String value; + + OpenstackAuthVersion(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + /** + * Compare the passed value with the internal value of the enum. + * + * @return: true if the strings are equal, false otherwise. + */ + public boolean compare(String value) { + if (value != null && !value.isEmpty()) { + return this.value.equals(value); + } + return false; + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/VmrcCredentials.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/VmrcCredentials.java new file mode 100644 index 0000000..9f6a91e --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/providers/VmrcCredentials.java @@ -0,0 +1,15 @@ +package es.upv.i3m.grycap.im.auth.credentials.providers; + +import es.upv.i3m.grycap.im.auth.credentials.ServiceProvider; + +public class VmrcCredentials extends GenericCredentials { + + private VmrcCredentials() { + super(ServiceProvider.VMRC); + } + + public static VmrcCredentials buildCredentials() { + return new VmrcCredentials(); + } + +} diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeaderTest.java similarity index 50% rename from src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java rename to src/test/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeaderTest.java index 06e7422..791537e 100644 --- a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java +++ b/src/test/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeaderTest.java @@ -1,23 +1,22 @@ -package es.upv.i3m.grycap.im.auth; +package es.upv.i3m.grycap.im.auth.credentials; import es.upv.i3m.grycap.ImTestWatcher; import es.upv.i3m.grycap.file.NoNullOrEmptyFile; import es.upv.i3m.grycap.file.Utf8File; import es.upv.i3m.grycap.im.InfrastructureManager; import es.upv.i3m.grycap.im.InfrastructureManagerTest; -import es.upv.i3m.grycap.im.auth.credential.Credential; -import es.upv.i3m.grycap.im.auth.credential.docker.DockerCredential; -import es.upv.i3m.grycap.im.auth.credential.dummy.DummyCredential; -import es.upv.i3m.grycap.im.auth.credential.ec2.AmazonEc2UserPwdCredential; -import es.upv.i3m.grycap.im.auth.credential.gce.GceUserPwdCredential; -import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImTokenCredential; -import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImUsernamePasswordCredential; -import es.upv.i3m.grycap.im.auth.credential.occi.OcciCredential; -import es.upv.i3m.grycap.im.auth.credential.opennebula.OpenNebulaTokenCredential; -import es.upv.i3m.grycap.im.auth.credential.opennebula.OpenNebulaUserPwdCredential; -import es.upv.i3m.grycap.im.auth.credential.openstack.OpenstackAuthVersion; -import es.upv.i3m.grycap.im.auth.credential.openstack.OpenstackCredential; -import es.upv.i3m.grycap.im.auth.credential.vmrc.VmrcCredential; +import es.upv.i3m.grycap.im.auth.credentials.providers.AmazonEc2Credentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.DockerCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.DummyCredential; +import es.upv.i3m.grycap.im.auth.credentials.providers.FogBowCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.GceCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.ImCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.KubernetesCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.OcciCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.OpenNebulaCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.OpenStackCredentials; +import es.upv.i3m.grycap.im.auth.credentials.providers.OpenstackAuthVersion; +import es.upv.i3m.grycap.im.auth.credentials.providers.VmrcCredentials; import es.upv.i3m.grycap.im.exceptions.ImClientException; import es.upv.i3m.grycap.im.pojo.InfrastructureUri; import es.upv.i3m.grycap.im.rest.client.BodyContentType; @@ -46,8 +45,15 @@ public class AuthorizationHeaderTest extends ImTestWatcher { "id = im ; type = InfrastructureManager ; token = token"; private static final String VMRC_CREDS = "id = vmrc ; type = VMRC ; " + USER_PASS + " ; host = host"; - private static final String OST_CREDS = "id = ost ; type = OpenStack ; " - + USER_PASS + " ; host = host ; service_region = region"; + private static final String OST_CREDS = + "id = ost ; type = OpenStack ; " + USER_PASS + + " ; tenant = tenant ; service_region = region ; host = host"; + private static final String OST_CREDS_PASS_3 = + "id = ost ; type = OpenStack ; " + USER_PASS + + " ; tenant = tenant ; service_region = region ; host = host ; auth_version = 3.X_password"; + private static final String OST_CREDS_PASS_3_TOKEN = + "id = ost ; type = OpenStack ; " + USER_PASS + + " ; tenant = tenant ; service_region = region ; host = host ; auth_version = 3.x_oidc_access_token"; private static final String OCCI_CREDS = "id = occi ; type = OCCI ; host = host ; proxy = proxy"; private static final String ONE_UP_CREDS = @@ -60,6 +66,10 @@ public class AuthorizationHeaderTest extends ImTestWatcher { "id = docker ; type = Docker ; host = host_url"; private static final String GCE_CREDS = "id = gce ; type = GCE ; " + USER_PASS + " ; project = testPrj"; + private static final String KUB_CREDS = + "id = kub ; type = Kubernetes ; username = user ; password = pass ; host = host"; + private static final String FOG_BOW_CREDS = + "id = fog ; type = FogBow ; host = host ; proxy = proxy"; // IM information private static final String IM_DUMMY_PROVIDER_URL = "http://servproject.i3m.upv.es:8811"; @@ -75,25 +85,26 @@ public static void createAuthorizationHeader() { @Before public void clearAuthorizationHeader() { - ah.setCredentialsAuthInfos(new ArrayList()); + ah.setCredentialsAuthInfos(new ArrayList()); } @Test public void testAuthorizationHeader() throws ImClientException { // Create authorization headers - Credential cred = ImUsernamePasswordCredential.getBuilder() - .withUsername("imuser01").withPassword("invitado").build(); + Credentials cred = ImCredentials.buildCredentials().withUsername("imuser01") + .withPassword("invitado"); ah.addCredential(cred); - cred = VmrcCredential.getBuilder().withUsername("demo").withPassword("demo") - .withHost("http://servproject.i3m.upv.es:8080/vmrc/vmrc").build(); + cred = VmrcCredentials.buildCredentials().withUsername("demo") + .withPassword("demo") + .withHost("http://servproject.i3m.upv.es:8080/vmrc/vmrc"); ah.addCredential(cred); - cred = DummyCredential.getBuilder().build(); + cred = DummyCredential.buildCredentials(); ah.addCredential(cred); // Check the headers work with the dummy provider try { InfrastructureManager im = - new InfrastructureManager(IM_DUMMY_PROVIDER_URL, ah.serialize()); + new InfrastructureManager(IM_DUMMY_PROVIDER_URL, ah); String toscaFile = new NoNullOrEmptyFile(new Utf8File(Paths.get(TOSCA_FILE_PATH))) .read(); @@ -111,93 +122,128 @@ public void testAuthorizationHeader() throws ImClientException { } } + @Test + public void testAmazonEc2UserPassCredentials() { + Credentials cred = AmazonEc2Credentials.buildCredentials() + .withUsername(USER).withPassword(PASS); + ah.addCredential(cred); + Assert.assertEquals(EC2_CREDS, ah.serialize()); + } + + @Test + public void testDockerCredentials() { + Credentials cred = + DockerCredentials.buildCredentials().withHost("host_url"); + ah.addCredential(cred); + Assert.assertEquals(DOCKER_CREDS, ah.serialize()); + } + @Test public void testDummyCredentials() { - Credential cred = DummyCredential.getBuilder().build(); + Credentials cred = DummyCredential.buildCredentials(); ah.addCredential(cred); Assert.assertEquals(DUMMY_CREDS, ah.serialize()); } + @Test + public void testFogBowCredentials() { + Credentials cred = FogBowCredentials.buildCredentials().withHost("host") + .withProxy("proxy"); + ah.addCredential(cred); + Assert.assertEquals(FOG_BOW_CREDS, ah.serialize()); + } + + @Test + public void testGceUserPwdCredentials() { + Credentials cred = GceCredentials.buildCredentials().withUsername(USER) + .withPassword(PASS).withProject("testPrj"); + ah.addCredential(cred); + Assert.assertEquals(GCE_CREDS, ah.serialize()); + } + @Test public void testImUserPassCredentials() { - Credential cred = ImUsernamePasswordCredential.getBuilder() - .withUsername(USER).withPassword(PASS).build(); + Credentials cred = + ImCredentials.buildCredentials().withUsername(USER).withPassword(PASS); ah.addCredential(cred); Assert.assertEquals(IM_UP_CREDS, ah.serialize()); } @Test public void testImTokenCredentials() { - Credential cred = - ImTokenCredential.getBuilder().withToken("token").build(); + Credentials cred = ImCredentials.buildCredentials().withToken("token"); ah.addCredential(cred); Assert.assertEquals(IM_TK_CREDS, ah.serialize()); } @Test - public void testVmrcCredentials() { - Credential cred = VmrcCredential.getBuilder().withUsername(USER) - .withPassword(PASS).withHost("host").build(); + public void testKubernetesCredentials() { + Credentials cred = KubernetesCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withHost("host"); ah.addCredential(cred); - Assert.assertEquals(VMRC_CREDS, ah.serialize()); + Assert.assertEquals(KUB_CREDS, ah.serialize()); } @Test - public void testOpenStackCredentials() { - Credential cred = - OpenstackCredential.getBuilder().withUsername(USER).withPassword(PASS) - .withTenant("tenant").withServiceRegion("region").withHost("host") - .withAuthVersion(OpenstackAuthVersion.PASSWORD_2_0).build(); + public void testOcciCredentials() { + Credentials cred = + OcciCredentials.buildCredentials().withHost("host").withProxy("proxy"); ah.addCredential(cred); - Assert.assertEquals(OST_CREDS, ah.serialize()); + Assert.assertEquals(OCCI_CREDS, ah.serialize()); } @Test public void testOpenNebulaUserPassCredentials() { - Credential cred = OpenNebulaUserPwdCredential.getBuilder() - .withUsername(USER).withPassword(PASS).withHost("host").build(); + Credentials cred = OpenNebulaCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withHost("host"); ah.addCredential(cred); Assert.assertEquals(ONE_UP_CREDS, ah.serialize()); } @Test public void testOpenNebulaTokenCredentials() { - Credential cred = OpenNebulaTokenCredential.getBuilder() - .withToken("token").withHost("host").build(); + Credentials cred = OpenNebulaCredentials.buildCredentials() + .withToken("token").withHost("host"); ah.addCredential(cred); Assert.assertEquals(ONE_TK_CREDS, ah.serialize()); } @Test - public void testOcciCredentials() { - Credential cred = - OcciCredential.getBuilder().withHost("host").withProxy("proxy").build(); + public void testOpenStackCredentials() { + Credentials cred = OpenStackCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withTenant("tenant") + .withServiceRegion("region").withHost("host") + .withAuthVersion(OpenstackAuthVersion.PASSWORD_2_0); ah.addCredential(cred); - Assert.assertEquals(OCCI_CREDS, ah.serialize()); + Assert.assertEquals(OST_CREDS, ah.serialize()); } @Test - public void testAmazonEc2UserPassCredentials() { - Credential cred = AmazonEc2UserPwdCredential.getBuilder() - .withUsername(USER).withPassword(PASS).build(); + public void testOpenStackCredentialsPassword3() { + Credentials cred = OpenStackCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withTenant("tenant") + .withServiceRegion("region").withHost("host") + .withAuthVersion(OpenstackAuthVersion.PASSWORD_3_X); ah.addCredential(cred); - Assert.assertEquals(EC2_CREDS, ah.serialize()); + Assert.assertEquals(OST_CREDS_PASS_3, ah.serialize()); } @Test - public void testDockerCredentials() { - Credential cred = - DockerCredential.getBuilder().withHost("host_url").build(); + public void testOpenStackCredentialsPassword3Token() { + Credentials cred = OpenStackCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withTenant("tenant") + .withServiceRegion("region").withHost("host") + .withAuthVersion(OpenstackAuthVersion.PASSWORD_3_X_TOKEN); ah.addCredential(cred); - Assert.assertEquals(DOCKER_CREDS, ah.serialize()); + Assert.assertEquals(OST_CREDS_PASS_3_TOKEN, ah.serialize()); } @Test - public void testGceUserPwdCredentials() { - Credential cred = GceUserPwdCredential.getBuilder().withUsername(USER) - .withPassword(PASS).withProject("testPrj").build(); + public void testVmrcCredentials() { + Credentials cred = VmrcCredentials.buildCredentials().withUsername(USER) + .withPassword(PASS).withHost("host"); ah.addCredential(cred); - Assert.assertEquals(GCE_CREDS, ah.serialize()); + Assert.assertEquals(VMRC_CREDS, ah.serialize()); } }