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 dd30f64..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java +++ /dev/null @@ -1,59 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -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 - public String serialize() { - return serialize(null).toString(); - } - - protected StringBuilder serialize(StringBuilder sb) { - if (sb == null) { - sb = new StringBuilder(); - } - if (!isNullOrEmpty(id)) { - sb.append("id = ").append(id).append(" ; "); - } - sb.append("type = ").append(getServiceType().getValue()); - return sb; - } - - //@formatter:off - public abstract static class AbstractCredentialBuilder - , T extends AbstractCredential> - 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 deleted file mode 100644 index 2dfcfff..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java +++ /dev/null @@ -1,49 +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(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; token = ").append(token); - return sb; - } - - //@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 8af35d6..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java +++ /dev/null @@ -1,81 +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(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - if (!isNullOrEmpty(username)) { - sb.append(" ; username = ").append(username); - } - if (!isNullOrEmpty(password)) { - sb.append(" ; password = ").append(password); - } - return sb; - } - - //@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(String username) { - this.username = username; - return (B) this; - } - - @SuppressWarnings("unchecked") - public B withPassword(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 ea5a157..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 ServiceType getServiceType(); - - public String serialize(); - - public interface CredentialBuilder { - - public T build(); - - } -} 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/DummyCredential.java deleted file mode 100644 index f1d9912..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java +++ /dev/null @@ -1,28 +0,0 @@ -package es.upv.i3m.grycap.im.auth.credential; - -public class DummyCredential extends AbstractCredential { - - protected DummyCredential(DummyCredentialBuilder builder) { - super(builder); - } - - @Override - public ServiceType getServiceType() { - return ServiceType.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/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/im/ImCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java deleted file mode 100644 index c584973..0000000 --- a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java +++ /dev/null @@ -1,64 +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.ServiceType; - -public final class ImCredential { - - public static class ImTokenCredential - extends AbstractTokenCredential { - - protected ImTokenCredential(ImTokenCredentialBuilder builder) { - super(builder); - } - - @Override - public ServiceType getServiceType() { - return ServiceType.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 { - @Override - public ServiceType getServiceType() { - return ServiceType.INFRASTRUCTURE_MANAGER; - } - - protected ImUsernamePasswordCredential( - ImUsernamePasswordCredentialBuilder builder) { - super(builder); - } - - 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 fa6bd00..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.ServiceType; - -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 ServiceType getServiceType() { - return ServiceType.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 a52e1dd..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.ServiceType; - -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 ServiceType getServiceType() { - return ServiceType.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 59980c1..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.ServiceType; - -public class OpenNebulaUserPwdCredential - extends AbstractUsernamePasswordCredential { - - private String host; - - 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.OPENNEBULA; - } - - @Override - public StringBuilder serialize(StringBuilder sb) { - sb = super.serialize(sb); - sb.append(" ; host = ").append(host); - return sb; - } - - protected OpenNebulaUserPwdCredential( - OpennebulaUserPwdCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - } - - 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 d69e148..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.ServiceType; - -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 ServiceType getServiceType() { - return ServiceType.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 506e964..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.ServiceType; - -public class VmrcCredential - extends AbstractUsernamePasswordCredential { - - private String host; - - protected VmrcCredential(VmrcCredentialBuilder builder) { - super(builder); - setHost(builder.getHost()); - - } - - @Override - public ServiceType getServiceType() { - return ServiceType.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 67% 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 e704e60..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/credentials/ServiceProvider.java b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/ServiceProvider.java new file mode 100644 index 0000000..f379283 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credentials/ServiceProvider.java @@ -0,0 +1,36 @@ +package es.upv.i3m.grycap.im.auth.credentials; + +public enum ServiceProvider { + + //@formatter:off + INFRASTRUCTURE_MANAGER("im","InfrastructureManager"), + VMRC("vmrc","VMRC"), + DUMMY("dummy","Dummy"), + OPENNEBULA("one","OpenNebula"), + EC2("ec2","EC2"), + FOG_BOW("fog","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/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/AuthorizationHeaderTest.java deleted file mode 100644 index 9903e39..0000000 --- a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java +++ /dev/null @@ -1,153 +0,0 @@ -package es.upv.i3m.grycap.im.auth; - -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.DummyCredential; -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.exceptions.ImClientException; -import es.upv.i3m.grycap.im.pojo.InfrastructureUri; -import es.upv.i3m.grycap.im.rest.client.BodyContentType; -import es.upv.i3m.grycap.logger.ImJavaApiLogger; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.nio.file.Paths; -import java.util.ArrayList; - -public class AuthorizationHeaderTest extends ImTestWatcher { - - private static final String DUMMY_CREDS = "id = dummy ; type = Dummy"; - private static final String IM_CREDS = - "type = InfrastructureManager ; username = imuser01 ; password = pwd"; - 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"; - 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"; - private static final String ONE_TK_CREDS = - "id = one ; type = OpenNebula ; token = token ; host = host"; - 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 AuthorizationHeader ah; - - @BeforeClass - public static void createAuthorizationHeader() { - ah = new AuthorizationHeader(); - } - - @Before - public void clearAuthorizationHeader() { - 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); - cred = VmrcCredential.getBuilder().withUsername("demo").withPassword("demo") - .withHost("http://servproject.i3m.upv.es:8080/vmrc/vmrc").build(); - authorizationHeader.addCredential(cred); - cred = DummyCredential.getBuilder().withId("dummy").build(); - authorizationHeader.addCredential(cred); - - // Check the headers work with the dummy provider - try { - InfrastructureManager im = new InfrastructureManager( - IM_DUMMY_PROVIDER_URL, authorizationHeader.serialize()); - String toscaFile = - new NoNullOrEmptyFile(new Utf8File(Paths.get(TOSCA_FILE_PATH))) - .read(); - InfrastructureUri newInfrastructureUri = - im.createInfrastructure(toscaFile, BodyContentType.TOSCA); - String uri = newInfrastructureUri.getUri(); - Assert.assertEquals(false, uri.isEmpty()); - String infId = newInfrastructureUri.getInfrastructureId(); - im.destroyInfrastructure(infId); - - } catch (ImClientException exception) { - ImJavaApiLogger.severe(InfrastructureManagerTest.class, - exception.getMessage()); - Assert.fail(); - } - } - - @Test - public void testDummyCredentials() throws ImClientException { - Credential cred = DummyCredential.getBuilder().withId("dummy").build(); - ah.addCredential(cred); - Assert.assertEquals(DUMMY_CREDS, ah.serialize()); - } - - @Test - public void testImCredentials() throws ImClientException { - Credential cred = ImUsernamePasswordCredential.getBuilder() - .withUsername("imuser01").withPassword("pwd").build(); - ah.addCredential(cred); - Assert.assertEquals(IM_CREDS, ah.serialize()); - } - - @Test - public void testVmrcCredentials() throws ImClientException { - Credential cred = VmrcCredential.getBuilder().withUsername("demo") - .withPassword("pwd").withHost("host").build(); - ah.addCredential(cred); - Assert.assertEquals(VMRC_CREDS, ah.serialize()); - } - - @Test - public void testOpenStackCredentials() throws ImClientException { - Credential cred = OpenstackCredential.getBuilder().withId("ost") - .withUsername("usr").withPassword("pwd").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() throws ImClientException { - Credential cred = OpenNebulaUserPwdCredential.getBuilder().withId("one") - .withUsername("usr").withPassword("pwd").withHost("host").build(); - ah.addCredential(cred); - Assert.assertEquals(ONE_UP_CREDS, ah.serialize()); - } - - @Test - public void testOpenNebulaTokenCredentials() throws ImClientException { - Credential cred = OpenNebulaTokenCredential.getBuilder().withId("one") - .withToken("token").withHost("host").build(); - ah.addCredential(cred); - Assert.assertEquals(ONE_TK_CREDS, ah.serialize()); - } - - @Test - public void testOcciCredentials() throws ImClientException { - Credential cred = OcciCredential.getBuilder().withId("occi") - .withHost("host").withProxy("proxy").build(); - ah.addCredential(cred); - Assert.assertEquals(OCCI_CREDS, ah.serialize()); - } - -} diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeaderTest.java new file mode 100644 index 0000000..791537e --- /dev/null +++ b/src/test/java/es/upv/i3m/grycap/im/auth/credentials/AuthorizationHeaderTest.java @@ -0,0 +1,249 @@ +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.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; +import es.upv.i3m.grycap.logger.ImJavaApiLogger; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.nio.file.Paths; +import java.util.ArrayList; + +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 = + "id = im ; type = InfrastructureManager ; " + USER_PASS; + private static final String IM_TK_CREDS = + "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 + + " ; 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 = + "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"; + 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"; + private static final String TOSCA_FILE_PATH = + "./src/test/resources/tosca/galaxy_tosca.yaml"; + + private static AuthorizationHeader ah; + + @BeforeClass + public static void createAuthorizationHeader() { + ah = new AuthorizationHeader(); + } + + @Before + public void clearAuthorizationHeader() { + ah.setCredentialsAuthInfos(new ArrayList()); + } + + @Test + public void testAuthorizationHeader() throws ImClientException { + // Create authorization headers + Credentials cred = ImCredentials.buildCredentials().withUsername("imuser01") + .withPassword("invitado"); + ah.addCredential(cred); + cred = VmrcCredentials.buildCredentials().withUsername("demo") + .withPassword("demo") + .withHost("http://servproject.i3m.upv.es:8080/vmrc/vmrc"); + ah.addCredential(cred); + cred = DummyCredential.buildCredentials(); + ah.addCredential(cred); + + // Check the headers work with the dummy provider + try { + InfrastructureManager im = + new InfrastructureManager(IM_DUMMY_PROVIDER_URL, ah); + String toscaFile = + new NoNullOrEmptyFile(new Utf8File(Paths.get(TOSCA_FILE_PATH))) + .read(); + InfrastructureUri newInfrastructureUri = + im.createInfrastructure(toscaFile, BodyContentType.TOSCA); + String uri = newInfrastructureUri.getUri(); + Assert.assertEquals(false, uri.isEmpty()); + String infId = newInfrastructureUri.getInfrastructureId(); + im.destroyInfrastructure(infId); + + } catch (ImClientException exception) { + ImJavaApiLogger.severe(InfrastructureManagerTest.class, + exception.getMessage()); + Assert.fail(); + } + } + + @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() { + 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() { + Credentials cred = + ImCredentials.buildCredentials().withUsername(USER).withPassword(PASS); + ah.addCredential(cred); + Assert.assertEquals(IM_UP_CREDS, ah.serialize()); + } + + @Test + public void testImTokenCredentials() { + Credentials cred = ImCredentials.buildCredentials().withToken("token"); + ah.addCredential(cred); + Assert.assertEquals(IM_TK_CREDS, ah.serialize()); + } + + @Test + public void testKubernetesCredentials() { + Credentials cred = KubernetesCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withHost("host"); + ah.addCredential(cred); + Assert.assertEquals(KUB_CREDS, ah.serialize()); + } + + @Test + public void testOcciCredentials() { + Credentials cred = + OcciCredentials.buildCredentials().withHost("host").withProxy("proxy"); + ah.addCredential(cred); + Assert.assertEquals(OCCI_CREDS, ah.serialize()); + } + + @Test + public void testOpenNebulaUserPassCredentials() { + Credentials cred = OpenNebulaCredentials.buildCredentials() + .withUsername(USER).withPassword(PASS).withHost("host"); + ah.addCredential(cred); + Assert.assertEquals(ONE_UP_CREDS, ah.serialize()); + } + + @Test + public void testOpenNebulaTokenCredentials() { + Credentials cred = OpenNebulaCredentials.buildCredentials() + .withToken("token").withHost("host"); + ah.addCredential(cred); + Assert.assertEquals(ONE_TK_CREDS, ah.serialize()); + } + + @Test + 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(OST_CREDS, ah.serialize()); + } + + @Test + 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(OST_CREDS_PASS_3, ah.serialize()); + } + + @Test + 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(OST_CREDS_PASS_3_TOKEN, ah.serialize()); + } + + @Test + public void testVmrcCredentials() { + Credentials cred = VmrcCredentials.buildCredentials().withUsername(USER) + .withPassword(PASS).withHost("host"); + ah.addCredential(cred); + Assert.assertEquals(VMRC_CREDS, ah.serialize()); + } + +}