d045135c49ff267cc5e71abf650c6c5abe4e4e37
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / impl / ChefApiHeaderFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.appc.adapter.chef.chefclient.impl;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableMap.Builder;
24 import java.util.Date;
25
26 public class ChefApiHeaderFactory {
27
28     private FormattedTimestamp formattedTimestamp = new FormattedTimestamp();
29
30     static {
31         System.setProperty("javax.net.ssl.trustStore", "/opt/onap/appc/chef/chefServerSSL.jks");
32         System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
33     }
34
35     public ImmutableMap<String, String> create(String methodName, String path, String body, String userId,
36         String organizations, String pemPath) {
37
38         String hashedBody = Utils.sha1AndBase64(body);
39         String timeStamp = formattedTimestamp.format(new Date());
40
41         Builder<String, String> builder = ImmutableMap.builder();
42         builder
43             .put("Content-type", "application/json")
44             .put("Accept", "application/json")
45             .put("X-Ops-Timestamp", timeStamp)
46             .put("X-Ops-UserId", userId)
47             .put("X-Chef-Version", "12.4.1")
48             .put("X-Ops-Content-Hash", hashedBody)
49             .put("X-Ops-Sign", "version=1.0")
50             .build();
51
52         String hashedPath = Utils.sha1AndBase64("/organizations/" + organizations + path);
53
54         StringBuilder sb = new StringBuilder();
55         sb.append("Method:").append(methodName).append("\n");
56         sb.append("Hashed Path:").append(hashedPath).append("\n");
57         sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
58         sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
59         sb.append("X-Ops-UserId:").append(userId);
60
61         String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
62         String[] auth_headers = Utils.splitAs60(auth_String);
63
64         for (int i = 0; i < auth_headers.length; i++) {
65             builder.put("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
66         }
67
68         return builder.build();
69     }
70
71 }