Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-55718 #218

Merged
merged 7 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public RestClient getRestClient() {
public boolean login() throws Exception {
boolean ret;
try {
ret = AuthenticationTool.authenticate(restClient, almLoginInfo.getUserName(),
ret = AuthenticationTool.getInstance().authenticate(restClient, almLoginInfo.getUserName(),
almLoginInfo.getPassword(), almLoginInfo.getServerUrl(), almLoginInfo.getClientType(), _logger);
} catch (Exception cause) {
ret = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public AUTEnvironmentBuilderPerformer(

public void start() {
try {
if (AuthenticationTool.authenticate(getClient(),
if (AuthenticationTool.getInstance().authenticate(getClient(),
model.getAlmUserName(),
model.getAlmPassword(),
model.getAlmServerUrl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Testsuites execute(RestClient client, Args args, Logger logger)
Testsuites ret = null;
_logger = logger;
_running = true;
if (AuthenticationTool.authenticate(client, args.getUsername(), args.getPassword(), args.getUrl(), args.getClientType(), logger)) {
if (AuthenticationTool.getInstance().authenticate(client, args.getUsername(), args.getPassword(), args.getUrl(), args.getClientType(), logger)) {
initialize(args, client);
if (start(args)) {
_polling = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Roy-Lu marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (c) 2012 Hewlett-Packard Development Company, L.P.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.microfocus.application.automation.tools.sse.sdk.authenticator;

import com.microfocus.adm.performancecenter.plugins.common.rest.RESTConstants;
import com.microfocus.application.automation.tools.sse.sdk.Client;
import com.microfocus.application.automation.tools.sse.sdk.Logger;
import com.microfocus.application.automation.tools.sse.sdk.ResourceAccessLevel;
import com.microfocus.application.automation.tools.sse.sdk.Response;

import java.util.HashMap;
import java.util.Map;

/**
* For ALM1260sso and 15sso, there's a different API to authenticated with api key.
*/
public class ApiKeyAuthenticator implements Authenticator {

private static final String APIKEY_LOGIN_API = "rest/oauth2/login";
private static final String CLIENT_TYPE = "ALM-CLIENT-TYPE";

@Override
public boolean login(Client client, String clientId, String secret, String clientType, Logger logger) {
logger.log("Start login to ALM server with APIkey...");
Map<String, String> headers = new HashMap<String, String>();
headers.put(CLIENT_TYPE, clientType);
headers.put(RESTConstants.ACCEPT, "application/json");
headers.put(RESTConstants.CONTENT_TYPE, "application/json");

Response response =
client.httpPost(
client.build(APIKEY_LOGIN_API),
String.format("{clientId:%s, secret:%s}", clientId, secret).getBytes(),
headers,
ResourceAccessLevel.PUBLIC);
boolean result = response.isOk();
logger.log(
result ? String.format(
"Logged in successfully to ALM Server %s using %s",
client.getServerUrl(),
clientId)
: String.format(
"Login to ALM Server at %s failed. Status Code: %s",
client.getServerUrl(),
response.getStatusCode()));
return result;
}

@Override
public boolean logout(Client client, String username) {
// No logout
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,43 @@

package com.microfocus.application.automation.tools.sse.sdk.authenticator;

import com.microfocus.adm.performancecenter.plugins.common.rest.RESTConstants;
import com.microfocus.application.automation.tools.common.SSEException;
import com.microfocus.application.automation.tools.sse.sdk.Client;
import com.microfocus.application.automation.tools.sse.sdk.Logger;
import com.microfocus.application.automation.tools.sse.sdk.ResourceAccessLevel;
import com.microfocus.application.automation.tools.sse.sdk.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Unify the rest authentication process here from separated part, ALMRestTool and RunManager.
* Any authentication change will only need to change here.
* Created by llu2 on 4/5/2017.
*/
public final class AuthenticationTool {
public class AuthenticationTool {

private static List<Authenticator> authenticators;
private List<Authenticator> authenticators;
private static AuthenticationTool instance;

private AuthenticationTool() {
// Add the private constructor to hide the implicit public one.
}

static {
authenticators = new ArrayList<>();
authenticators.add(new RestAuthenticator());
authenticators.add(new ApiKeyAuthenticator());
}

/**
* Try authenticate use a list of authenticators and then create session.
*/
public static boolean authenticate(Client client, String username, String password, String url, String clientType, Logger logger) {
if (login(client, username, password, url, logger)) {
appendQCSessionCookies(client, clientType, logger);
return true;
public static synchronized AuthenticationTool getInstance() {
if (instance == null) {
instance = new AuthenticationTool();
}
return false;
return instance;
}

private static boolean login(Client client, String username, String password, String url, Logger logger) {
/**
* Try authenticate use a list of authenticators and then create session.
*/
public boolean authenticate(Client client, String username, String password, String url, String clientType, Logger logger) {
boolean result = false;
for(Authenticator authenticator : authenticators) {
try {
result = authenticator.login(client, username, password, logger);
result = authenticator.login(client, username, password, clientType, logger);
if (result) {
break;
}
Expand All @@ -80,30 +71,4 @@ private static boolean login(Client client, String username, String password, St
}
return result;
}

private static void appendQCSessionCookies(Client client, String clientType, Logger logger) {
logger.log("Creating session...");

Map<String, String> headers = new HashMap<String, String>();
headers.put(RESTConstants.CONTENT_TYPE, RESTConstants.APP_XML);
headers.put(RESTConstants.ACCEPT, RESTConstants.APP_XML);

// issue a post request so that cookies relevant to the QC Session will be added to the RestClient
Response response =
client.httpPost(
client.build("rest/site-session"),
generateClientTypeData(clientType),
headers,
ResourceAccessLevel.PUBLIC);
if (!response.isOk()) {
throw new SSEException("Cannot append QCSession cookies", response.getFailure());
} else {
logger.log("Session created.");
}
}

private static byte[] generateClientTypeData(String clientType) {
String data = String.format("<session-parameters><client-type>%s</client-type></session-parameters>", clientType);
return data.getBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public interface Authenticator {

boolean login(Client client, String username, String password, Logger logger);
boolean login(Client client, String username, String password, String clientType, Logger logger);
boolean logout(Client client, String username);

}
Loading