@@ -2,24 +2,71 @@ package utils
2
2
3
3
import (
4
4
"context"
5
+ "crypto/tls"
6
+ "crypto/x509"
5
7
"fmt"
8
+ "os"
9
+ "path/filepath"
10
+ "strings"
11
+ "time"
6
12
7
13
rhtasv1alpha1 "github.com/securesign/operator/api/v1alpha1"
8
14
"github.com/securesign/operator/internal/controller/common/utils/kubernetes"
15
+ "google.golang.org/grpc"
16
+ "google.golang.org/grpc/credentials"
9
17
"sigs.k8s.io/controller-runtime/pkg/client"
10
18
)
11
19
12
- func UseTLS (instance * rhtasv1alpha1.Rekor ) bool {
20
+ // Mock used in tests
21
+ var MockUseTrillianTLS func (ctx context.Context , serviceAddr string , tlsCACertFile string ) (bool , error )
13
22
14
- if instance == nil {
15
- return false
23
+ // checks if trillian-logserver service supports TLS
24
+ func UseTrillianTLS (ctx context.Context , serviceAddr string , tlsCACertFile string ) (bool , error ) {
25
+
26
+ if MockUseTrillianTLS != nil {
27
+ return MockUseTrillianTLS (ctx , serviceAddr , "" )
28
+ }
29
+
30
+ if kubernetes .IsOpenShift () {
31
+ return true , nil
32
+ }
33
+
34
+ timeout := 5 * time .Second
35
+ ctx , cancel := context .WithTimeout (ctx , timeout )
36
+ defer cancel ()
37
+
38
+ hostname := serviceAddr
39
+ if idx := strings .Index (serviceAddr , ":" ); idx != - 1 {
40
+ hostname = serviceAddr [:idx ]
41
+ }
42
+
43
+ var creds credentials.TransportCredentials
44
+ if tlsCACertFile != "" {
45
+ tlsCaCert , err := os .ReadFile (filepath .Clean (tlsCACertFile ))
46
+ if err != nil {
47
+ return false , fmt .Errorf ("failed to load tls ca cert: %v" , err )
48
+ }
49
+ certPool := x509 .NewCertPool ()
50
+ if ! certPool .AppendCertsFromPEM (tlsCaCert ) {
51
+ return false , fmt .Errorf ("failed to append CA certificate to pool" )
52
+ }
53
+ creds = credentials .NewTLS (& tls.Config {
54
+ ServerName : hostname ,
55
+ RootCAs : certPool ,
56
+ MinVersion : tls .VersionTLS12 ,
57
+ })
58
+ }
59
+
60
+ conn , err := grpc .DialContext (ctx , serviceAddr , grpc .WithTransportCredentials (creds ), grpc .WithBlock ())
61
+ if err != nil {
62
+ fmt .Printf ("gRPC service at %s is not TLS secured: %v\n " , serviceAddr , err )
63
+ return false , nil
16
64
}
17
- // TLS enabled on Trillian logserver
18
- if instance .Spec .TrustedCA != nil || kubernetes .IsOpenShift () {
19
- return true
65
+ if err := conn .Close (); err != nil {
66
+ return false , fmt .Errorf ("failed to close connection: %v" , err )
20
67
}
21
68
22
- return false
69
+ return true , nil
23
70
}
24
71
25
72
func CAPath (ctx context.Context , cli client.Client , instance * rhtasv1alpha1.Rekor ) (string , error ) {
0 commit comments