aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java
blob: d2a0eb26f2e71dd973a1c8455bd55623240a80c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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;
	}
}