istio.io/istio@v0.0.0-20240520182934-d79c90f27776/security/pkg/nodeagent/caclient/providers/mock/mockcaclient.go (about) 1 // Copyright Istio Authors 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 mock 16 17 import ( 18 "encoding/pem" 19 "fmt" 20 "path" 21 "sync/atomic" 22 "time" 23 24 "istio.io/istio/security/pkg/pki/util" 25 ) 26 27 var ( 28 sampleKeyCertsPath = "../../../../samples/certs/" 29 caCertPath = path.Join(sampleKeyCertsPath, "ca-cert.pem") 30 caKeyPath = path.Join(sampleKeyCertsPath, "ca-key.pem") 31 certChainPath = []string{path.Join(sampleKeyCertsPath, "cert-chain.pem")} 32 rootCertPath = path.Join(sampleKeyCertsPath, "root-cert.pem") 33 ) 34 35 // CAClient is the mocked CAClient for testing. 36 type CAClient struct { 37 SignInvokeCount uint64 38 bundle *util.KeyCertBundle 39 certLifetime time.Duration 40 GeneratedCerts [][]string // Cache the generated certificates for verification purpose. 41 mockTrustAnchor bool 42 } 43 44 // NewMockCAClient creates an instance of CAClient. errors is used to specify the number of errors 45 // before CSRSign returns a valid response. certLifetime specifies the TTL for the newly issued workload cert. 46 func NewMockCAClient(certLifetime time.Duration, mockTrustAnchor bool) (*CAClient, error) { 47 cl := CAClient{ 48 SignInvokeCount: 0, 49 certLifetime: certLifetime, 50 mockTrustAnchor: mockTrustAnchor, 51 } 52 bundle, err := util.NewVerifiedKeyCertBundleFromFile(caCertPath, caKeyPath, certChainPath, rootCertPath) 53 if err != nil { 54 return nil, fmt.Errorf("mock ca client creation error: %v", err) 55 } 56 cl.bundle = bundle 57 58 atomic.StoreUint64(&cl.SignInvokeCount, 0) 59 return &cl, nil 60 } 61 62 func (c *CAClient) Close() {} 63 64 // CSRSign returns the certificate or errors depending on the settings. 65 func (c *CAClient) CSRSign(csrPEM []byte, certValidTTLInSec int64) ([]string, error) { 66 atomic.AddUint64(&c.SignInvokeCount, 1) 67 signingCert, signingKey, certChain, rootCert := c.bundle.GetAll() 68 csr, err := util.ParsePemEncodedCSR(csrPEM) 69 if err != nil { 70 return nil, fmt.Errorf("csr sign error: %v", err) 71 } 72 subjectIDs := []string{"test"} 73 certBytes, err := util.GenCertFromCSR(csr, signingCert, csr.PublicKey, *signingKey, subjectIDs, c.certLifetime, false) 74 if err != nil { 75 return nil, fmt.Errorf("csr sign error: %v", err) 76 } 77 78 block := &pem.Block{ 79 Type: "CERTIFICATE", 80 Bytes: certBytes, 81 } 82 cert := pem.EncodeToMemory(block) 83 84 ret := []string{string(cert), string(certChain), string(rootCert)} 85 c.GeneratedCerts = append(c.GeneratedCerts, ret) 86 return ret, nil 87 } 88 89 func (c *CAClient) GetRootCertBundle() ([]string, error) { 90 if c.mockTrustAnchor { 91 rootCertBytes := c.bundle.GetRootCertPem() 92 return []string{string(rootCertBytes)}, nil 93 } 94 95 return []string{}, nil 96 }