Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
target
bin
.metadata
**/integrationTest.properties
.idea
*.iml
**/integrationTest.properties
75 changes: 75 additions & 0 deletions src/main/java/com/openshift/client/IAuthorization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2014 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sean Kavanagh - initial API and implementation
******************************************************************************/
package com.openshift.client;

/**
* Operations to manage and view authorization resources
*
* @link http://openshift.github.io/documentation/rest_api/rest-api-1-6.html#
* authorization
*
* @author Sean Kavanagh
*/
public interface IAuthorization extends IOpenShiftResource {

public static String SCOPE_SESSION = "session";
public static String SCOPE_SESSION_READ = "session read";
public static int NO_EXPIRES_IN = -1;

/**
* Returns the unique id for this authorization.
*
* @return
*/
public String getId();

/**
* authorization note
*
* @return
*/
public String getNote();

/**
* returns the scope of the authorization token to determine type of access.
*
* @return
*/
public String getScopes();

/**
* Returns authorization string that contains user credentials.
*
* @return
*/
public String getToken();

/**
* Returns the total time in seconds before this authorization expires.
*
* @return
*/
public int getExpiresIn();

/**
* Destroys this authorization
*
* @throws OpenShiftException
*/
public void destroy() throws OpenShiftException;

/**
* Refreshes the authorization by reloading its content from OpenShift.
*
* @throws OpenShiftException
*/
public void refresh() throws OpenShiftException;
}
4 changes: 3 additions & 1 deletion src/main/java/com/openshift/client/IHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Red Hat, Inc.
* Copyright (c) 2011-2014 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
Expand All @@ -25,6 +25,7 @@
* @author André Dietisheim
* @author Nicolas Spano
* @author Corey Daley
* @author Sean Kavanagh
*/
public interface IHttpClient {

Expand All @@ -43,6 +44,7 @@ public interface IHttpClient {
public static final String MEDIATYPE_APPLICATION_FORMURLENCODED = "application/x-www-form-urlencoded";

public static final String AUTHORIZATION_BASIC = "Basic";
public static final String AUTHORIZATION_BEARER = "Bearer";

public static final int STATUS_OK = 200;
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
Expand Down
74 changes: 57 additions & 17 deletions src/main/java/com/openshift/client/IUser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Red Hat, Inc.
* Copyright (c) 2011-2014 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
Expand All @@ -12,50 +12,89 @@

import java.util.List;


/**
* @author André Dietisheim
* @author Sean Kavanagh
*/
public interface IUser extends IOpenShiftResource {

public String getId();
public String getId();

public String getRhlogin();

public String getPassword();

public String getServer();

public IOpenShiftConnection getConnection();

public IDomain createDomain(String id) throws OpenShiftException;

public List<IDomain> getDomains() throws OpenShiftException;

public IDomain getDefaultDomain() throws OpenShiftException;

public IDomain getDomain(String id) throws OpenShiftException;

public boolean hasDomain() throws OpenShiftException;

public boolean hasDomain(String id) throws OpenShiftException;

public List<IOpenShiftSSHKey> getSSHKeys() throws OpenShiftException;

/**
* Returns current authorization. Creates new authorization for user if none
* exists. Authorization is set by default when token is used to create API
* connection.
*
* @return authorization
* @throws OpenShiftException
*/
public IAuthorization getAuthorization() throws OpenShiftException;

/**
* Creates and returns new authorization set for user
*
* @param note
* A reminder description of what the authorization is for.
* @param scopes
* Scope of the authorization token to determine type of access.
* @return authorization
* @throws OpenShiftException
*/
public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException;

/**
* Creates and returns new authorization set for user
*
* @param note
* A reminder description of what the authorization is for.
* @param scopes
* Scope of the authorization token to determine type of access.
* @param expiresIn
* The number of seconds before this authorization expires.
* @return authorization
* @throws OpenShiftException
*/
public IAuthorization createAuthorization(String note, String scopes, int expiresIn) throws OpenShiftException;

/**
* Deprecated, use {@link #addSSHKey(String, ISSHPublicKey)}
*
* @param name key name to use
* @param key the key to put/add
* @param name
* key name to use
* @param key
* the key to put/add
* @return
* @throws OpenShiftException
*/
@Deprecated
public IOpenShiftSSHKey putSSHKey(String name, ISSHPublicKey key) throws OpenShiftException;

/**
* Adds the given ssh key with the given name. Key names and public keys have to be unique. Throws
* OpenShiftSSHKeyException if either the key name or the public key are already used.
* Adds the given ssh key with the given name. Key names and public keys
* have to be unique. Throws OpenShiftSSHKeyException if either the key name
* or the public key are already used.
*
* @param name
* the name to identify the key
Expand All @@ -67,18 +106,19 @@ public interface IUser extends IOpenShiftResource {
public IOpenShiftSSHKey addSSHKey(String name, ISSHPublicKey key) throws OpenShiftException;

public IOpenShiftSSHKey getSSHKeyByName(String name) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException;

public IOpenShiftSSHKey getSSHKeyByPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException;

public IOpenShiftSSHKey getSSHKeyByPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException,
OpenShiftException;

public boolean hasSSHKeyName(String name) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException;

public boolean hasSSHPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException, OpenShiftException;

public boolean removeSSHKey(String name);

@Deprecated
public void deleteKey(String name);

public int getMaxGears();

public int getConsumedGears();
Expand Down
74 changes: 64 additions & 10 deletions src/main/java/com/openshift/client/OpenShiftConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012 Red Hat, Inc.
* Copyright (c) 2012-2014 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
Expand Down Expand Up @@ -30,10 +30,12 @@
* @author Xavier Coulon
* @author Andre Dietisheim
* @author Corey Daley
* @author Sean Kavanagh
*
*/
public class OpenShiftConnectionFactory extends AbstractOpenShiftConnectionFactory {
private IOpenShiftConfiguration configuration = null;

/**
* Establish a connection with the clientId along with user's password.
* User's login and Server URL are retrieved from the local configuration
Expand Down Expand Up @@ -107,12 +109,16 @@ public IOpenShiftConnection getConnection(final String clientId, final String us

public IOpenShiftConnection getConnection(final String clientId, final String username, final String password,
final String serverUrl, ISSLCertificateCallback sslCallback) throws OpenShiftException {
return getConnection(clientId, username, password, null, null, serverUrl, sslCallback);
return getConnection(clientId, username, password, null, null, null, serverUrl, sslCallback);
}
public IOpenShiftConnection getConnection(final String clientId, final String token,
final String serverUrl, ISSLCertificateCallback sslCallback) throws OpenShiftException {
return getConnection(clientId, null, null, null, null, token, serverUrl, sslCallback);
}

public IOpenShiftConnection getConnection(final String clientId, final String username, final String password,
final String authKey, final String authIV, final String serverUrl) throws OpenShiftException {
return getConnection(clientId, username, password, null, null, serverUrl, null);
return getConnection(clientId, username, password, null, null, null, serverUrl, null);
}

/**
Expand All @@ -125,6 +131,8 @@ public IOpenShiftConnection getConnection(final String clientId, final String us
* user's login.
* @param password
* user's password.
* @param token
* authorization token.
* @param serverUrl
* the server url.
* @return a valid connection
Expand All @@ -133,7 +141,7 @@ public IOpenShiftConnection getConnection(final String clientId, final String us
* @throws OpenShiftException
*/
public IOpenShiftConnection getConnection(final String clientId, final String username, final String password,
final String authKey, final String authIV, final String serverUrl,
final String authKey, final String authIV, final String token, final String serverUrl,
final ISSLCertificateCallback sslCertificateCallback) throws OpenShiftException {
if (configuration == null) {
try {
Expand All @@ -144,31 +152,77 @@ public IOpenShiftConnection getConnection(final String clientId, final String us
}

Assert.notNull(clientId);
Assert.notNull(username);
Assert.notNull(password);
if (token == null || token.trim().length() == 0) {
Assert.notNull(username);
Assert.notNull(password);
}
Assert.notNull(serverUrl);

try {
IHttpClient httpClient =
new UrlConnectionHttpClientBuilder()
.setCredentials(username, password, authKey, authIV)
.setCredentials(username, password, authKey, authIV, token)
.setSSLCertificateCallback(sslCertificateCallback)
.setConfigTimeout(configuration.getTimeout())
.client();
return getConnection(clientId, username, password, serverUrl, httpClient);
return getConnection(clientId, username, password, token, serverUrl, httpClient);
} catch (IOException e) {
throw new OpenShiftException(e, "Failed to establish connection for user ''{0}}''", username);
}
}

protected IOpenShiftConnection getConnection(final String clientId, final String username, final String password,
protected IOpenShiftConnection getConnection(final String clientId, final String username, final String password, final String token,
final String serverUrl, IHttpClient httpClient) throws OpenShiftException, IOException {
Assert.notNull(clientId);
Assert.notNull(serverUrl);
Assert.notNull(httpClient);

IRestService service = new RestService(serverUrl, clientId, new JsonMediaType(),
IHttpClient.MEDIATYPE_APPLICATION_JSON, new OpenShiftJsonDTOFactory(), httpClient);
return getConnection(service, username, password);
return getConnection(service, username, password, token);
}

/**
* Establish a connection with the clientId along with a user's authorization token
*
* @param clientId
* http client id
* @param token
* authorization token.
* @param serverUrl
* the server url.
* @return a valid connection
* @throws FileNotFoundException
* @throws IOException
* @throws OpenShiftException
*/
public IOpenShiftConnection getAuthTokenConnection(final String clientId,final String token, final String serverUrl) throws OpenShiftException {
return getConnection(clientId, null, null, null, null, token, serverUrl, null);
}
/**
* Establish a connection with the clientId along with a user's authorization
* token. Server URL is retrieved from the local configuration file (in
* see $USER_HOME/.openshift/express.conf)
*
* @param clientId
* http client id
* @param token
* authorization token.
* @return a valid connection
* @throws FileNotFoundException
* @throws IOException
* @throws OpenShiftException
*/
public IOpenShiftConnection getAuthTokenConnection(final String clientId, final String token)
throws OpenShiftException {
try {
configuration = new OpenShiftConfiguration();
} catch (IOException e) {
throw new OpenShiftException(e, "Failed to load OpenShift configuration file.");
}
return getConnection(clientId, null, null, null, null, token, configuration.getLibraServer(), null);
}



}
Loading