2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.appc.adapter.chef.chefclient.impl;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableMap.Builder;
24 import java.util.Date;
26 public class ChefApiHeaderFactory {
28 private FormattedTimestamp formattedTimestamp = new FormattedTimestamp();
31 System.setProperty("javax.net.ssl.trustStore", "/opt/onap/appc/chef/chefServerSSL.jks");
32 System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
35 public ImmutableMap<String, String> create(String methodName, String path, String body, String userId,
36 String organizations, String pemPath) {
38 String hashedBody = Utils.sha1AndBase64(body);
39 String timeStamp = formattedTimestamp.format(new Date());
41 Builder<String, String> builder = ImmutableMap.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")
52 String hashedPath = Utils.sha1AndBase64("/organizations/" + organizations + path);
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);
61 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
62 String[] auth_headers = Utils.splitAs60(auth_String);
64 for (int i = 0; i < auth_headers.length; i++) {
65 builder.put("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
68 return builder.build();