2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.chef.chefapi;
24 import java.io.IOException;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.TimeZone;
29 import org.apache.http.*;
30 import org.apache.http.client.*;
31 import org.apache.http.client.methods.*;
32 import org.apache.http.impl.client.*;
33 import org.apache.http.util.EntityUtils;
34 import org.openecomp.appc.adapter.chef.chefclient.Utils;
36 import javax.net.ssl.SSLContext;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
40 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
41 import org.apache.http.impl.client.HttpClients;
42 import org.apache.http.ssl.SSLContexts;
44 public class ApiMethod {
45 private HttpClient client = null;
46 protected HttpRequestBase method = null;
47 protected HttpResponse response = null;
48 protected String reqBody = "";
49 protected String userId = "";
50 protected String pemPath = "";
51 protected String chefPath = "";
52 protected String organizations = "";
53 protected int resCode=0;
54 protected String responseBody="";
55 private String methodName = "GET";
56 public String test = "";
57 private int returnCode;
58 // final String KEY_STORE_PATH = "/tmp/chef/trusted_certs/mykeystore.jks";
59 // final String KEY_STORE_PASSWORD = "changeit";
61 public ApiMethod(String methodName) {
63 SSLContext sslcontext = SSLContexts.custom()
64 .loadTrustMaterial(new File(KEY_STORE_PATH), KEY_STORE_PASSWORD.toCharArray(),
65 new TrustSelfSignedStrategy())
67 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
69 new String[] { "TLSv1" },
71 SSLConnectionSocketFactory.getDefaultHostnameVerifier());
72 client = HttpClients.custom()
73 .setSSLSocketFactory(sslsf)
75 } catch (Exception e) {
76 // TODO Auto-generated catch block
79 client=HttpClients.createDefault();
80 this.methodName = methodName;
83 public ApiMethod execute() {
84 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
85 String hashedBody = Utils.sha1AndBase64(reqBody);
87 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
88 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
89 String timeStamp = sdf.format(new Date());
90 timeStamp = timeStamp.replace(" ", "T");
91 timeStamp = timeStamp + "Z";
93 StringBuilder sb = new StringBuilder();
94 sb.append("Method:").append(methodName).append("\n");
95 sb.append("Hashed Path:").append(hashedPath).append("\n");
96 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
97 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
98 sb.append("X-Ops-UserId:").append(userId);
99 test = test + "sb " + sb + "\n";
101 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
102 String[] auth_headers = Utils.splitAs60(auth_String);
104 method.addHeader("Content-type", "application/json");
105 method.addHeader("X-Ops-Timestamp", timeStamp);
106 method.addHeader("X-Ops-Userid", userId);
107 method.addHeader("X-Chef-Version", "12.4.1");
108 method.addHeader("Accept", "application/json");
109 method.addHeader("X-Ops-Content-Hash", hashedBody);
110 method.addHeader("X-Ops-Sign", "version=1.0");
112 for (int i = 0; i < auth_headers.length; i++) {
113 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
116 * test=test+this.method.getMethod()+"\n"; Header[]
117 * RHS=this.method.getHeaders(); for (int i = 0; i < RHS.length; i++) {
118 * test=test+RHS[i]+"\n"; } test=test+this.reqBody+"\n";
121 response = client.execute(method);
122 resCode = response.getStatusLine().getStatusCode();
123 HttpEntity entity1 = response.getEntity();
124 responseBody = EntityUtils.toString(entity1);}
127 responseBody=ex.getMessage();
132 public void setHeaders(Header[] headers) {
133 for (Header header : headers) {
134 this.method.addHeader(header);
138 public String getResponseBodyAsString() {
142 public int getReturnCode() {
146 public String getReqBody() {
150 public void setReqBody(String body) {
154 public String getUserId() {
158 public void setUserId(String userId) {
159 this.userId = userId;
162 public String getPemPath() {
166 public void setPemPath(String pemPath) {
167 this.pemPath = pemPath;
170 public String getChefPath() {
174 public void setChefPath(String chefPath) {
175 this.chefPath = chefPath;
178 public String getOrganizations() {
179 return organizations;
182 public void setOrganizations(String organizations) {
183 this.organizations = organizations;