This repository has been archived by the owner on May 1, 2020. It is now read-only.
/
secrets.go
91 lines (76 loc) · 3.89 KB
/
secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Copyright (C) 2019 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package containers
import (
"crypto/x509"
"encoding/pem"
"fmt"
horizonapi "github.com/blackducksoftware/horizon/pkg/api"
"github.com/blackducksoftware/horizon/pkg/components"
"github.com/blackducksoftware/synopsys-operator/pkg/util"
"github.com/sirupsen/logrus"
)
// GetSecrets will return the secrets
func (c *Creater) GetSecrets(cert string, key string, sealKey []byte) []*components.Secret {
var secrets []*components.Secret
certificateSecret := components.NewSecret(horizonapi.SecretConfig{Namespace: c.blackDuck.Spec.Namespace, Name: util.GetResourceName(c.blackDuck.Name, util.BlackDuckName, "webserver-certificate"), Type: horizonapi.SecretTypeOpaque})
certificateSecret.AddData(map[string][]byte{"WEBSERVER_CUSTOM_CERT_FILE": []byte(cert), "WEBSERVER_CUSTOM_KEY_FILE": []byte(key)})
certificateSecret.AddLabels(c.GetVersionLabel("secret"))
secrets = append(secrets, certificateSecret)
if len(c.blackDuck.Spec.ProxyCertificate) > 0 {
cert, err := c.stringToCertificate(c.blackDuck.Spec.ProxyCertificate)
if err != nil {
logrus.Warnf("The proxy certificate provided is invalid")
} else {
logrus.Debugf("Adding Proxy certificate with SN: %x", cert.SerialNumber)
proxyCertificateSecret := components.NewSecret(horizonapi.SecretConfig{Namespace: c.blackDuck.Spec.Namespace, Name: util.GetResourceName(c.blackDuck.Name, util.BlackDuckName, "proxy-certificate"), Type: horizonapi.SecretTypeOpaque})
proxyCertificateSecret.AddData(map[string][]byte{"HUB_PROXY_CERT_FILE": []byte(c.blackDuck.Spec.ProxyCertificate)})
proxyCertificateSecret.AddLabels(c.GetVersionLabel("secret"))
secrets = append(secrets, proxyCertificateSecret)
}
}
if len(c.blackDuck.Spec.AuthCustomCA) > 0 {
cert, err := c.stringToCertificate(c.blackDuck.Spec.AuthCustomCA)
if err != nil {
logrus.Warnf("The Auth Custom CA provided is invalid")
} else {
logrus.Debugf("Adding The Auth Custom CA with SN: %x", cert.SerialNumber)
authCustomCASecret := components.NewSecret(horizonapi.SecretConfig{Namespace: c.blackDuck.Spec.Namespace, Name: util.GetResourceName(c.blackDuck.Name, util.BlackDuckName, "auth-custom-ca"), Type: horizonapi.SecretTypeOpaque})
authCustomCASecret.AddData(map[string][]byte{"AUTH_CUSTOM_CA": []byte(c.blackDuck.Spec.AuthCustomCA)})
authCustomCASecret.AddLabels(c.GetVersionLabel("secret"))
secrets = append(secrets, authCustomCASecret)
}
}
// creating secrets to store the seal key
uploadCacheSecret := components.NewSecret(horizonapi.SecretConfig{Namespace: c.blackDuck.Spec.Namespace, Name: util.GetResourceName(c.blackDuck.Name, util.BlackDuckName, "upload-cache"), Type: horizonapi.SecretTypeOpaque})
uploadCacheSecret.AddData(map[string][]byte{"SEAL_KEY": sealKey})
uploadCacheSecret.AddLabels(c.GetVersionLabel("uploadcache"))
secrets = append(secrets, uploadCacheSecret)
return secrets
}
func (c *Creater) stringToCertificate(certificate string) (*x509.Certificate, error) {
block, _ := pem.Decode([]byte(certificate))
if block == nil {
return nil, fmt.Errorf("failed to parse certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}