2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.chef.chefapi;
27 import java.io.IOException;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.TimeZone;
32 import org.apache.http.*;
33 import org.apache.http.client.*;
34 import org.apache.http.client.methods.*;
35 import org.apache.http.impl.client.*;
36 import org.apache.http.util.EntityUtils;
37 import org.openecomp.appc.adapter.chef.chefclient.Utils;
39 import javax.net.ssl.SSLContext;
41 import org.apache.http.HttpEntity;
42 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
43 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
44 import org.apache.http.impl.client.HttpClients;
45 import org.apache.http.ssl.SSLContexts;
47 public class ApiMethod {
48 private HttpClient client = null;
49 protected HttpRequestBase method = null;
50 protected HttpResponse response = null;
51 protected String reqBody = "";
52 protected String userId = "";
53 protected String pemPath = "";
54 protected String chefPath = "";
55 protected String organizations = "";
56 protected int resCode=0;
57 protected String responseBody="";
58 private String methodName = "GET";
59 public String test = "";
60 private int returnCode;
61 final String KEY_STORE_PATH = "/etc/chef/trusted_certs/mykeystore.jks";
62 final String KEY_STORE_PASSWORD = "adminadmin";
65 System.setProperty("javax.net.ssl.trustStore", "/opt/app/bvc/chef/chefServerSSL.jks");
66 System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
68 public ApiMethod(String methodName) {
70 client=HttpClients.createDefault();
71 this.methodName = methodName;
74 public ApiMethod execute() {
75 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
76 String hashedBody = Utils.sha1AndBase64(reqBody);
78 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
79 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
80 String timeStamp = sdf.format(new Date());
81 timeStamp = timeStamp.replace(" ", "T");
82 timeStamp = timeStamp + "Z";
84 StringBuilder sb = new StringBuilder();
85 sb.append("Method:").append(methodName).append("\n");
86 sb.append("Hashed Path:").append(hashedPath).append("\n");
87 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
88 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
89 sb.append("X-Ops-UserId:").append(userId);
90 test = test + "sb " + sb + "\n";
92 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
93 String[] auth_headers = Utils.splitAs60(auth_String);
95 method.addHeader("Content-type", "application/json");
96 method.addHeader("X-Ops-Timestamp", timeStamp);
97 method.addHeader("X-Ops-Userid", userId);
98 method.addHeader("X-Chef-Version", "12.4.1");
99 method.addHeader("Accept", "application/json");
100 method.addHeader("X-Ops-Content-Hash", hashedBody);
101 method.addHeader("X-Ops-Sign", "version=1.0");
103 for (int i = 0; i < auth_headers.length; i++) {
104 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
108 response = client.execute(method);
109 resCode = response.getStatusLine().getStatusCode();
110 HttpEntity entity1 = response.getEntity();
111 responseBody = EntityUtils.toString(entity1);}
114 responseBody=ex.getMessage();
119 public void setHeaders(Header[] headers) {
120 for (Header header : headers) {
121 this.method.addHeader(header);
125 public String getResponseBodyAsString() {
129 public int getReturnCode() {
133 public String getReqBody() {
137 public void setReqBody(String body) {
141 public String getUserId() {
145 public void setUserId(String userId) {
146 this.userId = userId;
149 public String getPemPath() {
153 public void setPemPath(String pemPath) {
154 this.pemPath = pemPath;
157 public String getChefPath() {
161 public void setChefPath(String chefPath) {
162 this.chefPath = chefPath;
165 public String getOrganizations() {
166 return organizations;
169 public void setOrganizations(String organizations) {
170 this.organizations = organizations;