2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.chef.chefapi;
25 import java.io.IOException;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.TimeZone;
30 import org.apache.http.*;
31 import org.apache.http.client.*;
32 import org.apache.http.client.methods.*;
33 import org.apache.http.impl.client.*;
34 import org.apache.http.util.EntityUtils;
35 import org.openecomp.appc.adapter.chef.chefclient.Utils;
37 import javax.net.ssl.SSLContext;
39 import org.apache.http.HttpEntity;
40 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
41 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
42 import org.apache.http.impl.client.HttpClients;
43 import org.apache.http.ssl.SSLContexts;
45 public class ApiMethod {
46 private HttpClient client = null;
47 protected HttpRequestBase method = null;
48 protected HttpResponse response = null;
49 protected String reqBody = "";
50 protected String userId = "";
51 protected String pemPath = "";
52 protected String chefPath = "";
53 protected String organizations = "";
54 protected int resCode=0;
55 protected String responseBody="";
56 private String methodName = "GET";
57 public String test = "";
58 private int returnCode;
59 final String KEY_STORE_PATH = "/etc/chef/trusted_certs/mykeystore.jks";
60 final String KEY_STORE_PASSWORD = "adminadmin";
63 System.setProperty("javax.net.ssl.trustStore", "/opt/app/bvc/chef/chefServerSSL.jks");
64 System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
66 public ApiMethod(String methodName) {
68 client=HttpClients.createDefault();
69 this.methodName = methodName;
72 public ApiMethod execute() {
73 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
74 String hashedBody = Utils.sha1AndBase64(reqBody);
76 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
77 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
78 String timeStamp = sdf.format(new Date());
79 timeStamp = timeStamp.replace(" ", "T");
80 timeStamp = timeStamp + "Z";
82 StringBuilder sb = new StringBuilder();
83 sb.append("Method:").append(methodName).append("\n");
84 sb.append("Hashed Path:").append(hashedPath).append("\n");
85 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
86 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
87 sb.append("X-Ops-UserId:").append(userId);
88 test = test + "sb " + sb + "\n";
90 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
91 String[] auth_headers = Utils.splitAs60(auth_String);
93 method.addHeader("Content-type", "application/json");
94 method.addHeader("X-Ops-Timestamp", timeStamp);
95 method.addHeader("X-Ops-Userid", userId);
96 method.addHeader("X-Chef-Version", "12.4.1");
97 method.addHeader("Accept", "application/json");
98 method.addHeader("X-Ops-Content-Hash", hashedBody);
99 method.addHeader("X-Ops-Sign", "version=1.0");
101 for (int i = 0; i < auth_headers.length; i++) {
102 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
106 response = client.execute(method);
107 resCode = response.getStatusLine().getStatusCode();
108 HttpEntity entity1 = response.getEntity();
109 responseBody = EntityUtils.toString(entity1);}
112 responseBody=ex.getMessage();
117 public void setHeaders(Header[] headers) {
118 for (Header header : headers) {
119 this.method.addHeader(header);
123 public String getResponseBodyAsString() {
127 public int getReturnCode() {
131 public String getReqBody() {
135 public void setReqBody(String body) {
139 public String getUserId() {
143 public void setUserId(String userId) {
144 this.userId = userId;
147 public String getPemPath() {
151 public void setPemPath(String pemPath) {
152 this.pemPath = pemPath;
155 public String getChefPath() {
159 public void setChefPath(String chefPath) {
160 this.chefPath = chefPath;
163 public String getOrganizations() {
164 return organizations;
167 public void setOrganizations(String organizations) {
168 this.organizations = organizations;