e7b7d6fa0ad6cae30cc986f5c811887136136767
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.appc.adapter.chef.chefapi;
24
25 import java.io.IOException;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.TimeZone;
29
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;
36
37 import javax.net.ssl.SSLContext;
38 import java.io.File;
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;
44
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";
61         static
62 {
63         System.setProperty("javax.net.ssl.trustStore", "/opt/app/bvc/chef/chefServerSSL.jks");
64         System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
65                 }
66         public ApiMethod(String methodName) {
67
68                 client=HttpClients.createDefault();
69                 this.methodName = methodName;
70         }
71
72         public ApiMethod execute() {
73                 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
74                 String hashedBody = Utils.sha1AndBase64(reqBody);
75
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";
81
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";
89
90                 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
91                 String[] auth_headers = Utils.splitAs60(auth_String);
92
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");
100
101                 for (int i = 0; i < auth_headers.length; i++) {
102                         method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
103                 }
104
105                 try{
106                 response = client.execute(method);
107                 resCode = response.getStatusLine().getStatusCode();
108                 HttpEntity entity1 = response.getEntity();
109                 responseBody = EntityUtils.toString(entity1);}
110                 catch(Exception ex){
111                         resCode=500;
112                         responseBody=ex.getMessage();
113                 }
114                 return this;
115         }
116
117         public void setHeaders(Header[] headers) {
118                 for (Header header : headers) {
119                         this.method.addHeader(header);
120                 }
121         }
122
123         public String getResponseBodyAsString() {
124                 return responseBody;
125         }
126
127         public int getReturnCode() {
128                 return resCode;
129         }
130
131         public String getReqBody() {
132                 return reqBody;
133         }
134
135         public void setReqBody(String body) {
136                 this.reqBody = body;
137         }
138
139         public String getUserId() {
140                 return userId;
141         }
142
143         public void setUserId(String userId) {
144                 this.userId = userId;
145         }
146
147         public String getPemPath() {
148                 return pemPath;
149         }
150
151         public void setPemPath(String pemPath) {
152                 this.pemPath = pemPath;
153         }
154
155         public String getChefPath() {
156                 return chefPath;
157         }
158
159         public void setChefPath(String chefPath) {
160                 this.chefPath = chefPath;
161         }
162
163         public String getOrganizations() {
164                 return organizations;
165         }
166
167         public void setOrganizations(String organizations) {
168                 this.organizations = organizations;
169         }
170 }