14f14f61bf8593238fde50d36a72240a89f57a26
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-connection-plugin / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / K8sAbstractRestClientService.kt
1 /*
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.
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  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s
21
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
31
32 abstract class K8sAbstractRestClientService(
33     private val k8sConfiguration: K8sConnectionPluginConfiguration
34 ) : BlueprintWebClientService {
35
36     protected val baseUrl: String = k8sConfiguration.getProperties().url
37     private var restClientProperties: BasicAuthRestClientProperties? = null
38
39     private fun getBasicAuthRestClientProperties(): BasicAuthRestClientProperties {
40         return if (restClientProperties != null)
41             restClientProperties!!
42         else {
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
50         }
51     }
52
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"
59         return mapOfHeaders
60     }
61
62     private fun setBasicAuth(username: String, password: String): String {
63         val credentialsString = "$username:$password"
64         return Base64.getEncoder().encodeToString(credentialsString.toByteArray(Charset.defaultCharset()))
65     }
66
67     override fun defaultHeaders(): Map<String, String> {
68         val encodedCredentials = setBasicAuth(
69             getBasicAuthRestClientProperties().username,
70             getBasicAuthRestClientProperties().password
71         )
72         return mapOf(
73             CONTENT_TYPE to APPLICATION_JSON_VALUE,
74             ACCEPT to APPLICATION_JSON_VALUE,
75             AUTHORIZATION to "Basic $encodedCredentials"
76         )
77     }
78
79     override fun host(uri: String): String {
80         return getBasicAuthRestClientProperties().url + uri
81     }
82
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()))
87
88         if (!headers.containsKey(AUTHORIZATION)) {
89             val encodedCredentials = setBasicAuth(
90                 getBasicAuthRestClientProperties().username,
91                 getBasicAuthRestClientProperties().password
92             )
93             customHeaders[AUTHORIZATION] = "Basic $encodedCredentials"
94         }
95         return super.convertToBasicHeaders(customHeaders)
96     }
97
98     abstract fun apiUrl(): String
99 }