Fixes for Chef Adapter bundle
[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  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
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  */
21 package org.onap.appc.adapter.chef.chefclient.impl;
22
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.ImmutableMap.Builder;
25 import java.util.Date;
26
27 public class ChefApiHeaderFactory {
28
29     private FormattedTimestamp formattedTimestamp = new FormattedTimestamp();
30
31     public ImmutableMap<String, String> create(String methodName, String path, String body, String userId,
32         String organizations, String pemPath) {
33
34         String hashedBody = Utils.sha1AndBase64(body);
35         String timeStamp = formattedTimestamp.format(new Date());
36
37         Builder<String, String> builder = ImmutableMap.builder();
38         builder
39             .put("Content-type", "application/json")
40             .put("Accept", "application/json")
41             .put("X-Ops-Timestamp", timeStamp)
42             .put("X-Ops-UserId", userId)
43             .put("X-Chef-Version", "12.4.1")
44             .put("X-Ops-Content-Hash", hashedBody)
45             .put("X-Ops-Sign", "version=1.0")
46             .build();
47
48         String hashedPath = Utils.sha1AndBase64("/organizations/" + organizations + path);
49
50         StringBuilder sb = new StringBuilder();
51         sb.append("Method:").append(methodName).append("\n");
52         sb.append("Hashed Path:").append(hashedPath).append("\n");
53         sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
54         sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
55         sb.append("X-Ops-UserId:").append(userId);
56
57         String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
58         String[] auth_headers = Utils.splitAs60(auth_String);
59
60         for (int i = 0; i < auth_headers.length; i++) {
61             builder.put("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
62         }
63
64         return builder.build();
65     }
66
67 }