[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactor provider code
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / testdata / provider.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2020-2021 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
21 package testdata
22
23 import (
24         cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
25         "k8s.io/api/core/v1"
26         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27         "k8s.io/apimachinery/pkg/runtime"
28         "k8s.io/apimachinery/pkg/types"
29         scheme2 "k8s.io/client-go/kubernetes/scheme"
30         "sigs.k8s.io/controller-runtime/pkg/reconcile"
31
32         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
33 )
34
35 const (
36         SecretName           = "issuer-cert-secret"
37         Url                  = "https://oom-cert-service:8443/v1/certificate/"
38         HealthEndpoint       = "actuator/health"
39         CertEndpoint         = "v1/certificate"
40         CaName               = "RA"
41         KeySecretKey         = "cmpv2Issuer-key.pem"
42         CertSecretKey        = "cmpv2Issuer-cert.pem"
43         CacertSecretKey      = "cacert.pem"
44         Namespace            = "onap"
45         IssuerObjectName     = "cmpv2-issuer"
46         Kind                 = "CMPv2Issuer"
47         APIVersion           = "v1"
48         PrivateKeySecret     = "privateKeySecretName"
49         OldCertificateConfig = "{\"apiVersion\":\"cert-manager.io/v1\",\"kind\":\"Certificate\",\"metadata\":{\"annotations\":{},\"name\":\"cert-test\",\"namespace\":\"onap\"},\"spec\":{\"commonName\":\"certissuer.onap.org\",\"dnsNames\":[\"localhost\",\"certissuer.onap.org\"],\"emailAddresses\":[\"onap@onap.org\"],\"ipAddresses\":[\"127.0.0.1\"],\"issuerRef\":{\"group\":\"certmanager.onap.org\",\"kind\":\"CMPv2Issuer\",\"name\":\"cmpv2-issuer-onap\"},\"secretName\":\"cert-test-secret-name\",\"subject\":{\"countries\":[\"US\"],\"localities\":[\"San-Francisco\"],\"organizationalUnits\":[\"ONAP\"],\"organizations\":[\"Linux-Foundation\"],\"provinces\":[\"California\"]},\"uris\":[\"onap://cluster.local/\"]}}\n"
50 )
51
52 func GetValidIssuerWithSecret() (cmpv2api.CMPv2Issuer, v1.Secret) {
53         issuer := cmpv2api.CMPv2Issuer{
54                 TypeMeta: metav1.TypeMeta{
55                         APIVersion: APIVersion,
56                         Kind:       Kind,
57                 },
58                 ObjectMeta: metav1.ObjectMeta{
59                         Name:      IssuerObjectName,
60                         Namespace: Namespace,
61                 },
62                 Spec: GetValidCMPv2IssuerSpec(),
63         }
64
65         secret := v1.Secret{
66                 Data: map[string][]byte{
67                         KeySecretKey:    KeyBytes,
68                         CertSecretKey:   CertBytes,
69                         CacertSecretKey: CacertBytes,
70                 },
71                 ObjectMeta: metav1.ObjectMeta{
72                         Name:      SecretName,
73                         Namespace: Namespace,
74                 },
75         }
76         secret.Name = SecretName
77         return issuer, secret
78 }
79
80 func GetValidCMPv2IssuerSpec() cmpv2api.CMPv2IssuerSpec {
81         issuerSpec := cmpv2api.CMPv2IssuerSpec{
82                 URL:            Url,
83                 HealthEndpoint: HealthEndpoint,
84                 CertEndpoint:   CertEndpoint,
85                 CaName:         CaName,
86                 CertSecretRef: cmpv2api.SecretKeySelector{
87                         Name:      SecretName,
88                         KeyRef:    KeySecretKey,
89                         CertRef:   CertSecretKey,
90                         CacertRef: CacertSecretKey,
91                 },
92         }
93         return issuerSpec
94 }
95
96 func GetScheme() *runtime.Scheme {
97         scheme := runtime.NewScheme()
98         _ = scheme2.AddToScheme(scheme)
99         _ = cmapi.AddToScheme(scheme)
100         _ = cmpv2api.AddToScheme(scheme)
101         return scheme
102 }
103
104 func GetFakeRequest(objectName string) reconcile.Request {
105         fakeRequest := reconcile.Request{
106                 NamespacedName: CreateIssuerNamespaceName(Namespace, objectName),
107         }
108         return fakeRequest
109 }
110
111 func GetIssuerStoreKey() types.NamespacedName {
112         return CreateIssuerNamespaceName(Namespace, IssuerObjectName)
113 }
114
115 func CreateIssuerNamespaceName(namespace string, name string) types.NamespacedName {
116         return types.NamespacedName{
117                 Namespace: namespace,
118                 Name:      name,
119         }
120 }
121
122 func GetValidCertificateSecret() *v1.Secret {
123         const privateKeySecretKey = "tls.key"
124         const certificateSecretKey = "tls.crt"
125
126         return &v1.Secret{
127                 Data: map[string][]byte{
128                         privateKeySecretKey:  []byte("test-private-key"),
129                         certificateSecretKey: []byte("test-certificate"),
130                 },
131                 ObjectMeta: metav1.ObjectMeta{
132                         Name:      "cert-test-secret-name",
133                         Namespace: "onap",
134                 },
135         }
136 }
137