aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java
diff options
context:
space:
mode:
Diffstat (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java')
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java185
1 files changed, 185 insertions, 0 deletions
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java
new file mode 100644
index 000000000..d2a0eb26f
--- /dev/null
+++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java
@@ -0,0 +1,185 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.adapter.chef.chefapi;
+
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.apache.http.*;
+import org.apache.http.client.*;
+import org.apache.http.client.methods.*;
+import org.apache.http.impl.client.*;
+import org.apache.http.util.EntityUtils;
+import org.openecomp.appc.adapter.chef.chefclient.Utils;
+
+import javax.net.ssl.SSLContext;
+import java.io.File;
+import org.apache.http.HttpEntity;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.ssl.SSLContexts;
+
+public class ApiMethod {
+ private HttpClient client = null;
+ protected HttpRequestBase method = null;
+ protected HttpResponse response = null;
+ protected String reqBody = "";
+ protected String userId = "";
+ protected String pemPath = "";
+ protected String chefPath = "";
+ protected String organizations = "";
+ protected int resCode=0;
+ protected String responseBody="";
+ private String methodName = "GET";
+ public String test = "";
+ private int returnCode;
+// final String KEY_STORE_PATH = "/tmp/chef/trusted_certs/mykeystore.jks";
+// final String KEY_STORE_PASSWORD = "changeit";
+
+ public ApiMethod(String methodName) {
+/* try {
+ SSLContext sslcontext = SSLContexts.custom()
+ .loadTrustMaterial(new File(KEY_STORE_PATH), KEY_STORE_PASSWORD.toCharArray(),
+ new TrustSelfSignedStrategy())
+ .build();
+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
+ sslcontext,
+ new String[] { "TLSv1" },
+ null,
+ SSLConnectionSocketFactory.getDefaultHostnameVerifier());
+ client = HttpClients.custom()
+ .setSSLSocketFactory(sslsf)
+ .build();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }*/
+ client=HttpClients.createDefault();
+ this.methodName = methodName;
+ }
+
+ public ApiMethod execute() {
+ String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
+ String hashedBody = Utils.sha1AndBase64(reqBody);
+
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
+ String timeStamp = sdf.format(new Date());
+ timeStamp = timeStamp.replace(" ", "T");
+ timeStamp = timeStamp + "Z";
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("Method:").append(methodName).append("\n");
+ sb.append("Hashed Path:").append(hashedPath).append("\n");
+ sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
+ sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
+ sb.append("X-Ops-UserId:").append(userId);
+ test = test + "sb " + sb + "\n";
+
+ String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
+ String[] auth_headers = Utils.splitAs60(auth_String);
+
+ method.addHeader("Content-type", "application/json");
+ method.addHeader("X-Ops-Timestamp", timeStamp);
+ method.addHeader("X-Ops-Userid", userId);
+ method.addHeader("X-Chef-Version", "12.4.1");
+ method.addHeader("Accept", "application/json");
+ method.addHeader("X-Ops-Content-Hash", hashedBody);
+ method.addHeader("X-Ops-Sign", "version=1.0");
+
+ for (int i = 0; i < auth_headers.length; i++) {
+ method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
+ }
+ /*
+ * test=test+this.method.getMethod()+"\n"; Header[]
+ * RHS=this.method.getHeaders(); for (int i = 0; i < RHS.length; i++) {
+ * test=test+RHS[i]+"\n"; } test=test+this.reqBody+"\n";
+ */
+ try{
+ response = client.execute(method);
+ resCode = response.getStatusLine().getStatusCode();
+ HttpEntity entity1 = response.getEntity();
+ responseBody = EntityUtils.toString(entity1);}
+ catch(Exception ex){
+ resCode=500;
+ responseBody=ex.getMessage();
+ }
+ return this;
+ }
+
+ public void setHeaders(Header[] headers) {
+ for (Header header : headers) {
+ this.method.addHeader(header);
+ }
+ }
+
+ public String getResponseBodyAsString() {
+ return responseBody;
+ }
+
+ public int getReturnCode() {
+ return resCode;
+ }
+
+ public String getReqBody() {
+ return reqBody;
+ }
+
+ public void setReqBody(String body) {
+ this.reqBody = body;
+ }
+
+ public String getUserId() {
+ return userId;
+ }
+
+ public void setUserId(String userId) {
+ this.userId = userId;
+ }
+
+ public String getPemPath() {
+ return pemPath;
+ }
+
+ public void setPemPath(String pemPath) {
+ this.pemPath = pemPath;
+ }
+
+ public String getChefPath() {
+ return chefPath;
+ }
+
+ public void setChefPath(String chefPath) {
+ this.chefPath = chefPath;
+ }
+
+ public String getOrganizations() {
+ return organizations;
+ }
+
+ public void setOrganizations(String organizations) {
+ this.organizations = organizations;
+ }
+}