MCPcopy
hub / github.com/opencloud-eu/opencloud / persistKey

Function persistKey

pkg/crypto/gencert.go:109–130  ·  view source on GitHub ↗

persistKey persists the private key used to generate the certificate at the configured location.

(destination string, l log.Logger, pk any)

Source from the content-addressed store, hash-verified

107
108// persistKey persists the private key used to generate the certificate at the configured location.
109func persistKey(destination string, l log.Logger, pk any) error {
110 if err := ensureExistsDir(destination); err != nil {
111 return fmt.Errorf("creating key destination: %s", destination)
112 }
113
114 keyOut, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
115 if err != nil {
116 return fmt.Errorf("failed to open %v for writing", destination)
117 }
118 err = pem.Encode(keyOut, pemBlockForKey(pk, l))
119 if err != nil {
120 return fmt.Errorf("failed to encode key")
121 }
122
123 err = keyOut.Close()
124 if err != nil {
125 return fmt.Errorf("failed to write key")
126 }
127 l.Info().Msg(fmt.Sprintf("written key to %v", destination))
128
129 return nil
130}
131
132func publicKey(pk any) any {
133 switch k := pk.(type) {

Callers 2

GenCertFunction · 0.85
TestPersistKeyFunction · 0.85

Calls 5

ensureExistsDirFunction · 0.85
pemBlockForKeyFunction · 0.85
ErrorfMethod · 0.80
EncodeMethod · 0.65
CloseMethod · 0.65

Tested by 1

TestPersistKeyFunction · 0.68