Store certificates for a given API response and domain
(domain string, response *api.CertificateResponse)
| 53 | |
| 54 | // Store certificates for a given API response and domain |
| 55 | func (c *CertStorage) Store(domain string, response *api.CertificateResponse) error { |
| 56 | // Find disk locations |
| 57 | certFile, _ := c.absoluteFileName(domain, certificate) |
| 58 | keyFile, _ := c.absoluteFileName(domain, privateKey) |
| 59 | |
| 60 | locationContent := map[string]string{ |
| 61 | certFile: response.Certificate, |
| 62 | keyFile: response.PrivateKey, |
| 63 | } |
| 64 | |
| 65 | log.Infof("Attempting to write cert data for %s", domain) |
| 66 | |
| 67 | for location, content := range locationContent { |
| 68 | fh, err := os.OpenFile(location, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) |
| 69 | |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | _, err = fh.WriteString(content) |
| 75 | |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | err = fh.Sync() |
| 81 | |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | err = fh.Close() |
| 87 | |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | log.Infof("Successfully wrote cert data for %s", domain) |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (c *CertStorage) absoluteFileName(domain string, keyType int) (string, error) { |
| 98 | base := c.storageDirectory + "/" + strings.ToLower(domain) |
no test coverage detected