2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
4 * Modifications Copyright © 2021 Orange.
5 * Modifications Copyright © 2020 Deutsche Telekom AG.
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.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s
22 import org.apache.http.message.BasicHeader
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
25 import org.springframework.http.HttpHeaders.ACCEPT
26 import org.springframework.http.HttpHeaders.AUTHORIZATION
27 import org.springframework.http.HttpHeaders.CONTENT_TYPE
28 import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
29 import java.nio.charset.Charset
30 import java.util.Base64
32 abstract class K8sAbstractRestClientService(
33 private val k8sConfiguration: K8sConnectionPluginConfiguration
34 ) : BlueprintWebClientService {
36 protected val baseUrl: String = k8sConfiguration.getProperties().url
37 private var restClientProperties: BasicAuthRestClientProperties? = null
39 private fun getBasicAuthRestClientProperties(): BasicAuthRestClientProperties {
40 return if (restClientProperties != null)
41 restClientProperties!!
43 val basicAuthRestClientProperties = BasicAuthRestClientProperties()
44 basicAuthRestClientProperties.username = k8sConfiguration.getProperties().username
45 basicAuthRestClientProperties.password = k8sConfiguration.getProperties().password
46 basicAuthRestClientProperties.url = apiUrl()
47 basicAuthRestClientProperties.additionalHeaders = getHeaders()
48 restClientProperties = basicAuthRestClientProperties
49 return basicAuthRestClientProperties
53 private fun getHeaders(): HashMap<String, String> {
54 val mapOfHeaders = hashMapOf<String, String>()
55 mapOfHeaders["Accept"] = "application/json"
56 mapOfHeaders["Content-Type"] = "application/json"
57 mapOfHeaders["cache-control"] = " no-cache"
58 mapOfHeaders["Accept"] = "application/json"
62 private fun setBasicAuth(username: String, password: String): String {
63 val credentialsString = "$username:$password"
64 return Base64.getEncoder().encodeToString(credentialsString.toByteArray(Charset.defaultCharset()))
67 override fun defaultHeaders(): Map<String, String> {
68 val encodedCredentials = setBasicAuth(
69 getBasicAuthRestClientProperties().username,
70 getBasicAuthRestClientProperties().password
73 CONTENT_TYPE to APPLICATION_JSON_VALUE,
74 ACCEPT to APPLICATION_JSON_VALUE,
75 AUTHORIZATION to "Basic $encodedCredentials"
79 override fun host(uri: String): String {
80 return getBasicAuthRestClientProperties().url + uri
83 override fun convertToBasicHeaders(headers: Map<String, String>): Array<BasicHeader> {
84 val customHeaders: MutableMap<String, String> = headers.toMutableMap()
85 // inject additionalHeaders
86 customHeaders.putAll(verifyAdditionalHeaders(getBasicAuthRestClientProperties()))
88 if (!headers.containsKey(AUTHORIZATION)) {
89 val encodedCredentials = setBasicAuth(
90 getBasicAuthRestClientProperties().username,
91 getBasicAuthRestClientProperties().password
93 customHeaders[AUTHORIZATION] = "Basic $encodedCredentials"
95 return super.convertToBasicHeaders(customHeaders)
98 abstract fun apiUrl(): String