Debian Codegraph @

auth/internal/transport/cert/secureconnect_cert_test.go

raw
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package cert
16
17 import (
18 "bytes"
19 "errors"
20 "os"
21 "path/filepath"
22 "testing"
23 )
24
25 func TestSecureConnectSource_ConfigMissing(t *testing.T) {
26 source, err := NewSecureConnectProvider("missing.json")
27 if got, want := err, errSourceUnavailable; !errors.Is(err, errSourceUnavailable) {
28 t.Fatalf("got %v, want %v", got, want)
29 }
30 if source != nil {
31 t.Errorf("got %v, want nil source", source)
32 }
33 }
34
35 func TestSecureConnectSource_ConfigNotDirMissing(t *testing.T) {
36 source, err := NewSecureConnectProvider("/dev/null/missing.json")
37 if got, want := err, errSourceUnavailable; !errors.Is(err, errSourceUnavailable) {
38 t.Fatalf("got %v, want %v", got, want)
39 }
40 if source != nil {
41 t.Errorf("got %v, want nil source", source)
42 }
43 }
44
45 func TestSecureConnectSource_ConfigMissingPerms(t *testing.T) {
46 if os.Getuid() == 0 {
47 t.Skip("skipping permissions-related test because UID is 0 (reads never get EPERM while running as root in the current namespace)")
48 }
49 td := t.TempDir()
50 tmpFilePath := filepath.Join(td, "unreadable.json")
51 if wrErr := os.WriteFile(tmpFilePath, []byte{}, 0000); wrErr != nil {
52 t.Fatalf("failed to write temp file with permissions 000: %s", wrErr)
53 }
54 source, err := NewSecureConnectProvider(tmpFilePath)
55 if got, want := err, errSourceUnavailable; !errors.Is(err, errSourceUnavailable) {
56 t.Fatalf("got %v, want %v", got, want)
57 }
58 if source != nil {
59 t.Errorf("got %v, want nil source", source)
60 }
61 }
62
63 func TestSecureConnectSource_GetClientCertificateSuccess(t *testing.T) {
64 source, err := NewSecureConnectProvider("testdata/context_aware_metadata.json")
65 if err != nil {
66 t.Fatal(err)
67 }
68 cert, err := source(nil)
69 if err != nil {
70 t.Fatal(err)
71 }
72 if cert.Certificate == nil {
73 t.Fatal("got nil, want non-nil Certificate")
74 }
75 if cert.PrivateKey == nil {
76 t.Fatal("got nil, want non-nil PrivateKey")
77 }
78 }
79
80 func TestSecureConnectSource_GetClientCertificateFailure(t *testing.T) {
81 source, err := NewSecureConnectProvider("testdata/context_aware_metadata_invalid_pem.json")
82 if err != nil {
83 t.Fatal(err)
84 }
85 if _, err := source(nil); err == nil {
86 t.Error("got nil, want non-nil err")
87 }
88 }
89
90 func TestSecureConnectSource_ValidateMetadataSuccess(t *testing.T) {
91 metadata := secureConnectMetadata{Cmd: []string{"cat", "testdata/testcert.pem"}}
92 if err := validateMetadata(metadata); err != nil {
93 t.Fatal(err)
94 }
95 }
96
97 func TestSecureConnectSource_ValidateMetadataFailure(t *testing.T) {
98 metadata := secureConnectMetadata{Cmd: []string{}}
99 err := validateMetadata(metadata)
100 if err == nil {
101 t.Fatal("got nil, want non-nil err")
102 }
103 if got, want := err.Error(), "empty cert_provider_command"; got != want {
104 t.Errorf("got %v, want %v err", got, want)
105 }
106 }
107
108 func TestSecureConnectSource_IsCertificateExpiredTrue(t *testing.T) {
109 source, err := NewSecureConnectProvider("testdata/context_aware_metadata.json")
110 if err != nil {
111 t.Fatal(err)
112 }
113 cert, err := source(nil)
114 if err != nil {
115 t.Fatal(err)
116 }
117 if !isCertificateExpired(cert) {
118 t.Error("isCertificateExpired() = false, want true")
119 }
120 }
121
122 func TestSecureConnectSource_IsCertificateExpiredFalse(t *testing.T) {
123 source, err := NewSecureConnectProvider("testdata/context_aware_metadata_nonexpiring_pem.json")
124 if err != nil {
125 t.Fatal(err)
126 }
127 cert, err := source(nil)
128 if err != nil {
129 t.Fatal(err)
130 }
131 if isCertificateExpired(cert) {
132 t.Error("isCertificateExpired() = true, want false")
133 }
134 }
135
136 func TestCertificateCaching(t *testing.T) {
137 source := secureConnectSource{metadata: secureConnectMetadata{Cmd: []string{"cat", "testdata/nonexpiring.pem"}}}
138 cert, err := source.getClientCertificate(nil)
139 if err != nil {
140 t.Fatal(err)
141 }
142 if cert == nil {
143 t.Fatal("getClientCertificate() = nil, want non-nil cert")
144 }
145 if source.cachedCert == nil {
146 t.Fatal("got nil, want non-nil cachedCert")
147 }
148 if got, want := source.cachedCert.Certificate[0], cert.Certificate[0]; !bytes.Equal(got, want) {
149 t.Fatalf("got %v, want %v cached Certificate", got, want)
150 }
151 if got, want := source.cachedCert.PrivateKey, cert.PrivateKey; got != want {
152 t.Fatalf("got %v, want %v cached PrivateKey", got, want)
153 }
154 }