persistKey persists the private key used to generate the certificate at the configured location.
(destination string, l log.Logger, pk any)
| 107 | |
| 108 | // persistKey persists the private key used to generate the certificate at the configured location. |
| 109 | func 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 | |
| 132 | func publicKey(pk any) any { |
| 133 | switch k := pk.(type) { |