├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── certs.go ├── gen.go ├── main.go ├── options.go ├── renew.go ├── server.go └── user.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | os: 4 | - linux 5 | - osx 6 | 7 | env: 8 | - ARCH=x86_64 9 | - ARCH=i686 10 | 11 | go: 12 | - 1.5.3 13 | - 1.6 14 | 15 | script: 16 | - go vet ./... 17 | - diff -au <(gofmt -d *.go) <(printf "") 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ***DEPRECATED - This project is deprecated and not maintained anymore.*** 2 | ***It is recommended all users use https://certbot.eff.org/ instead.*** 3 | 4 | # Concert [![Slack](https://slack.minio.io/slack?type=svg)](https://slack.minio.io) 5 | 6 | Concert is a console based certificate generation tool for [letsencrypt.org](https://letsencrypt.org/). `Let’s Encrypt` is a free (as in free beer), automated, and open certificate authority. 7 | 8 | ### Prerequisite 9 | 10 | * A valid domain name purchased from any domain registrar. 11 | * `root` access to the server pointed by the domain name. 12 | * Working email address for the domain. 13 | 14 | ### Download 15 | 16 | We **STRONGLY RECOMMEND** installing `concert` from source, because it requires root access. Download pre-built binaries from [here](https://github.com/minio/concert/releases). 17 | 18 | ### Compile from Source (RECOMMENDED) 19 | 20 | We are assuming that you have installed golang already, run the following command to download and install `concert` from source. 21 | 22 | ```sh 23 | go get -u github.com/minio/concert 24 | ``` 25 | 26 | ### How to generate a certificate? 27 | 28 | To generate a certificate and key for `example.com`, run the following command on `example.com` server as `root`, under `my-certs` directory. 29 | 30 | ```sh 31 | sudo concert gen --dir my-certs admin@example.com example.com 32 | sudo ls my-certs 33 | certs.json public.crt private.key 34 | ``` 35 | 36 | NOTE: Generated certificates are valid only for a maximum of 90 days. Please visit the following link for more details - [https://letsencrypt.org/2015/11/09/why-90-days.html](https://letsencrypt.org/2015/11/09/why-90-days.html) 37 | 38 | ## How to generate a certificate bundle for various sub domains? 39 | 40 | To generate certificates for `example.com` and its sub domains ‘www’, ‘ftp’ and ‘mail’, use `sub-domains` command line option. You need to run this command as `root` on the `example.com` server. 41 | 42 | ```sh 43 | sudo concert gen --sub-domains www,ftp,mail admin@example.com example.com 44 | ``` 45 | 46 | Successfully generated bundled certs for sub domains ‘www’, ‘ftp’ and ‘mail’. 47 | 48 | ```bash 49 | sudo ls certs 50 | certs.json public.crt private.key 51 | ``` 52 | 53 | ## How to renew a certificate? 54 | 55 | To renew a certificate for example.com under ‘certs’ directory. New certs are generated and saved in the same directory as before. 56 | 57 | ```sh 58 | sudo concert renew admin@example.com 59 | ``` 60 | 61 | ### How to automatically renew certificates? 62 | 63 | You can run `concert` in server mode to automatically renew certificates, once in every 45 days. 64 | 65 | ```sh 66 | sudo concert server --dir my-certs admin@example.com example.com 67 | ``` 68 | 69 | ## How to automatically renew certificates for various sub domains? 70 | 71 | To automatically renew cerificates for `example.com` and its sub domains ‘www’, ‘ftp’ and ‘mail’, use `sub-domains` command line option. 72 | 73 | ```sh 74 | sudo concert server --sub-domains www,ftp,mail admin@example.com example.com 75 | ``` 76 | 77 | ### FAQ 78 | 79 | * Why `concert` requires root access? 80 | 81 | ACME protocol requires root access to verify authenticity of the domain ownership. During the certification generation phase, `concert` temporarily listens on port `80` or `443` to allow letsencrypt.org service connect and verify the ownership. Only `root` is allowed to bind to any port below `1024`. 82 | 83 | * Can I run `concert` as non-root? 84 | 85 | On GNU/Linux, it is possible to run as non-root by granting bind only access to `concert`. 86 | 87 | ```sh 88 | sudo setcap cap_net_bind_service=+ep `which concert` 89 | ``` 90 | -------------------------------------------------------------------------------- /certs.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "crypto/rand" 21 | "crypto/rsa" 22 | "encoding/json" 23 | "fmt" 24 | "io/ioutil" 25 | "os" 26 | "path/filepath" 27 | "strings" 28 | "time" 29 | 30 | "github.com/xenolf/lego/acme" 31 | ) 32 | 33 | // renewDaysLimit - renewal is not initated if cert is valid with in this limit. 34 | const renewDaysLimit = 45 // Number of days. 35 | 36 | // getCert expiration time. 37 | func getCertExpTime(certsDir string) (time.Time, error) { 38 | certBytes, err := loadCert(certsDir) 39 | if err != nil { 40 | return time.Time{}, err 41 | } 42 | return acme.GetPEMCertExpiration(certBytes) 43 | } 44 | 45 | // isValidDomain validates if input string is a valid domain name. 46 | func isValidDomain(host string) bool { 47 | // See RFC 1035, RFC 3696. 48 | host = strings.TrimSpace(host) 49 | if len(host) == 0 || len(host) > 255 { 50 | return false 51 | } 52 | // host cannot start or end with "-" 53 | if host[len(host)-1:] == "-" || host[:1] == "-" { 54 | return false 55 | } 56 | // host cannot start or end with "_" 57 | if host[len(host)-1:] == "_" || host[:1] == "_" { 58 | return false 59 | } 60 | // host cannot start or end with a "." 61 | if host[len(host)-1:] == "." || host[:1] == "." { 62 | return false 63 | } 64 | // All non alphanumeric characters are invalid. 65 | if strings.ContainsAny(host, "`~!@#$%^&*()+={}[]|\\\"';:> 2 78 | } 79 | 80 | // generate certificates. 81 | func genCerts(email, domain string, subDomains []string, sanDomains []string) (acme.CertificateResource, error) { 82 | // Create a user. New accounts need an email and private key to start with. 83 | const rsaKeySize = 2048 84 | privateKey, err := rsa.GenerateKey(rand.Reader, rsaKeySize) 85 | if err != nil { 86 | return acme.CertificateResource{}, err 87 | } 88 | 89 | // Initialize user. 90 | user := conUser{ 91 | Email: email, 92 | key: privateKey, 93 | } 94 | 95 | // A client facilitates communication with the CA server. 96 | client, err := acme.NewClient(acmeServer, &user, acme.RSA2048) 97 | if err != nil { 98 | return acme.CertificateResource{}, err 99 | } 100 | 101 | client.ExcludeChallenges([]acme.Challenge{acme.DNS01}) 102 | 103 | // New users will need to register; be sure to save it 104 | reg, err := client.Register() 105 | if err != nil { 106 | return acme.CertificateResource{}, err 107 | } 108 | user.Registration = reg 109 | 110 | // The client has a URL to the current Let's Encrypt Subscriber 111 | // Agreement. The user will need to agree to it. 112 | err = client.AgreeToTOS() 113 | if err != nil { 114 | return acme.CertificateResource{}, err 115 | } 116 | 117 | domains := []string{domain} 118 | if !isSubDomain(domain) { 119 | for _, subDomain := range subDomains { 120 | domains = append(domains, subDomain+"."+domain) 121 | } 122 | } 123 | 124 | // Append SAN Domains 125 | for _, sanDomain := range sanDomains { 126 | domains = append(domains, sanDomain) 127 | } 128 | 129 | // The acme library takes care of completing the challenges to 130 | // obtain the certificate(s). Of course, the hostnames must 131 | // resolve to this machine or it will fail. 132 | isBundle := true // Bundle all domains into one. 133 | newCertificates, failures := client.ObtainCertificate(domains, isBundle, nil, true) 134 | if len(failures) > 0 { 135 | var failedDomains []string 136 | var failedDomainsErrors []error 137 | for failedDomain, failedDomainErr := range failures { 138 | failedDomains = append(failedDomains, failedDomain) 139 | failedDomainsErrors = append(failedDomainsErrors, failedDomainErr) 140 | } 141 | failure := fmt.Errorf("Failed to obtain certificates for Domains: %s, with following errors %s respectively.", failedDomains, failedDomainsErrors) 142 | return acme.CertificateResource{}, failure 143 | } 144 | return newCertificates, nil 145 | } 146 | 147 | // Renew certificates. 148 | func renewCerts(certsDir, email string) (acme.CertificateResource, error) { 149 | certBytes, err := loadCert(certsDir) 150 | if err != nil { 151 | return acme.CertificateResource{}, err 152 | } 153 | 154 | expTime, err := acme.GetPEMCertExpiration(certBytes) 155 | expTimeDays := int(expTime.Sub(time.Now()).Hours() / 24.0) 156 | if expTimeDays > renewDaysLimit { 157 | return acme.CertificateResource{}, fmt.Errorf("Keys have not expired yet, please renew in %d days.", expTimeDays) 158 | } 159 | 160 | // Create a user. New accounts need an email and private key to start with. 161 | const rsaKeySize = 2048 162 | privateKey, err := rsa.GenerateKey(rand.Reader, rsaKeySize) 163 | if err != nil { 164 | return acme.CertificateResource{}, err 165 | } 166 | 167 | // Initialize user. 168 | user := conUser{ 169 | Email: email, 170 | key: privateKey, 171 | } 172 | 173 | // A client facilitates communication with the CA server. This CA 174 | // URL is configured for a local dev instance of Boulder running 175 | // in Docker in a VM. 176 | client, err := acme.NewClient(acmeServer, &user, acme.RSA2048) 177 | if err != nil { 178 | return acme.CertificateResource{}, err 179 | } 180 | 181 | client.ExcludeChallenges([]acme.Challenge{acme.DNS01}) 182 | 183 | certMeta, err := loadCertMeta(certsDir) 184 | if err != nil { 185 | return acme.CertificateResource{}, err 186 | } 187 | 188 | // Save current cert bytes. 189 | certMeta.Certificate = certBytes 190 | 191 | isBundle := true // Bundle all domains into one. 192 | newCertificates, err := client.RenewCertificate(certMeta, isBundle, true) 193 | if err != nil { 194 | return acme.CertificateResource{}, err 195 | } 196 | return newCertificates, nil 197 | } 198 | 199 | // load certificate meta resource. 200 | func loadCertMeta(certsDir string) (acme.CertificateResource, error) { 201 | metaBytes, err := ioutil.ReadFile(filepath.Join(certsDir, "certs.json")) 202 | if err != nil { 203 | return acme.CertificateResource{}, err 204 | } 205 | var certRes acme.CertificateResource 206 | err = json.Unmarshal(metaBytes, &certRes) 207 | if err != nil { 208 | return acme.CertificateResource{}, err 209 | } 210 | return certRes, nil 211 | } 212 | 213 | // load certs. 214 | func loadCert(certsDir string) ([]byte, error) { 215 | return ioutil.ReadFile(filepath.Join(certsDir, "public.crt")) 216 | } 217 | 218 | // saveCerts saves the certificates to disk. This includes the 219 | // certificate file itself, the private key, and the json metadata file. 220 | func saveCerts(certsDir string, cert acme.CertificateResource) error { 221 | // Save cert file. 222 | err := ioutil.WriteFile(filepath.Join(certsDir, "public.crt"), cert.Certificate, 0600) 223 | if err != nil { 224 | return err 225 | } 226 | 227 | // Save private key. 228 | err = ioutil.WriteFile(filepath.Join(certsDir, "private.key"), cert.PrivateKey, 0600) 229 | if err != nil { 230 | return err 231 | } 232 | 233 | // Save cert metadata. 234 | jsonBytes, err := json.MarshalIndent(&cert, "", "\t") 235 | if err != nil { 236 | return err 237 | } 238 | 239 | err = ioutil.WriteFile(filepath.Join(certsDir, "certs.json"), jsonBytes, 0600) 240 | if err != nil { 241 | return err 242 | } 243 | 244 | // Return success. 245 | return nil 246 | } 247 | 248 | // Verify if certs are available in a certs dir. 249 | func isCertAvailable(certsDir string) bool { 250 | _, crtErr := os.Stat(filepath.Join(certsDir, "public.crt")) 251 | _, keyErr := os.Stat(filepath.Join(certsDir, "private.key")) 252 | return crtErr == nil && keyErr == nil 253 | } 254 | -------------------------------------------------------------------------------- /gen.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "errors" 21 | "log" 22 | "net" 23 | "os" 24 | "strings" 25 | "time" 26 | 27 | "github.com/minio/cli" 28 | ) 29 | 30 | // ACME CA url. -- TODO make this configurable. 31 | const ( 32 | acmeStagingServer = "https://acme-staging.api.letsencrypt.org/directory" 33 | acmeServer = "https://acme-v01.api.letsencrypt.org/directory" 34 | ) 35 | 36 | func checkFolder(path string) error { 37 | if _, err := os.Stat(path); os.IsNotExist(err) { 38 | return os.MkdirAll(path, 0700) 39 | } 40 | return nil 41 | } 42 | 43 | // main for gen command. 44 | func genMain(c *cli.Context) { 45 | if !c.Args().Present() || c.Args().First() == "help" { 46 | cli.ShowCommandHelpAndExit(c, "gen", 1) // last argument is exit code 47 | } 48 | 49 | // Create certs dir. 50 | certsDir := c.String("dir") 51 | if err := checkFolder(certsDir); err != nil { 52 | log.Fatalln(err) 53 | } 54 | // Sub domains value. 55 | subDomainsValue := c.String("sub-domains") 56 | var subDomains []string 57 | if len(subDomainsValue) > 0 { 58 | subDomains = strings.Split(subDomainsValue, ",") 59 | } 60 | // SAN domains value. 61 | sanDomainsValue := c.String("san-domains") 62 | var sanDomains []string 63 | if len(sanDomainsValue) > 0 { 64 | sanDomains = strings.Split(sanDomainsValue, ",") 65 | } 66 | 67 | // Get email and domain. 68 | email := c.Args().Get(0) 69 | domain := c.Args().Get(1) 70 | 71 | // Validate if its a valid domain. 72 | if !isValidDomain(domain) { 73 | log.Fatalln(&net.DNSError{Err: "Invalid domain name", Name: domain}) 74 | } 75 | // Validate if domain is already a sub domain, error if 76 | // sub-domains option is specified. 77 | if isSubDomain(domain) && len(subDomainsValue) > 0 { 78 | log.Fatalln(errors.New("Domain is already a subdomain, SSL certs not allowed for sub sub domains.")) 79 | } 80 | newCertificates, err := genCerts(email, domain, subDomains, sanDomains) 81 | if err != nil { 82 | log.Fatalln(err) 83 | } 84 | 85 | // Each certificate comes back with the cert bytes, the bytes of 86 | // the client's private key, and a certificate URL. This is where 87 | // you should save them to files! 88 | err = saveCerts(certsDir, newCertificates) 89 | if err != nil { 90 | log.Fatalln(err) 91 | } 92 | expTime, err := getCertExpTime(certsDir) 93 | if err != nil { 94 | log.Fatalln(err) 95 | } 96 | log.Printf("Generated certificates for %s under %s will expire in %d days.\n", domain, certsDir, int(expTime.Sub(time.Now()).Hours()/24.0)) 97 | } 98 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "io/ioutil" 21 | "log" 22 | 23 | "github.com/minio/cli" 24 | "github.com/xenolf/lego/acme" 25 | ) 26 | 27 | func init() { 28 | // Disable acme logging. 29 | acme.Logger = log.New(ioutil.Discard, "", log.Lshortfile) 30 | } 31 | 32 | func main() { 33 | app := cli.NewApp() 34 | app.Usage = "Console certificate generation tool" 35 | app.Version = "0.0.3" 36 | app.Commands = commands 37 | app.Author = "Minio, Inc." 38 | 39 | app.RunAndExitOnError() 40 | } 41 | -------------------------------------------------------------------------------- /options.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import "github.com/minio/cli" 20 | 21 | var commands = []cli.Command{ 22 | genCmd, 23 | renewCmd, 24 | serverCmd, 25 | } 26 | 27 | var genCmd = cli.Command{ 28 | Name: "gen", 29 | Usage: "Generate certificates (valid for 90 days).", 30 | Flags: []cli.Flag{ 31 | cli.StringFlag{ 32 | Name: "dir", 33 | Value: "certs", // Default. 34 | Usage: "Generated certs destination directory. [DEFAULT: \"certs\"]", 35 | }, 36 | cli.StringFlag{ 37 | Name: "sub-domains", 38 | Usage: "Generate certs for requested sub-domains.", 39 | }, 40 | cli.StringFlag{ 41 | Name: "san-domains", 42 | Usage: "Generate certs for requested san-domains.", 43 | }, 44 | }, 45 | Action: genMain, 46 | } 47 | 48 | var renewCmd = cli.Command{ 49 | Name: "renew", 50 | Usage: "Renew certificates (valid for 90 days).", 51 | Flags: []cli.Flag{ 52 | cli.StringFlag{ 53 | Name: "dir", 54 | Value: "certs", // Default. 55 | }, 56 | }, 57 | Action: renewMain, 58 | } 59 | 60 | var serverCmd = cli.Command{ 61 | Name: "server", 62 | Usage: "Run in server mode to automatically renew certificate, once in every 45 days.", 63 | Flags: []cli.Flag{ 64 | cli.StringFlag{ 65 | Name: "dir", 66 | Value: "certs", // Default. 67 | Usage: "Generated certs destination directory. [DEFAULT: \"certs\"]", 68 | }, 69 | cli.StringFlag{ 70 | Name: "sub-domains", 71 | Usage: "Generate certs for requested sub-domains.", 72 | }, 73 | cli.StringFlag{ 74 | Name: "san-domains", 75 | Usage: "Generate certs for requested san-domains.", 76 | }, 77 | }, 78 | Action: serverMain, 79 | } 80 | -------------------------------------------------------------------------------- /renew.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "log" 21 | 22 | "github.com/minio/cli" 23 | ) 24 | 25 | // main for renew command. 26 | func renewMain(c *cli.Context) { 27 | if !c.Args().Present() || c.Args().First() == "help" { 28 | cli.ShowCommandHelpAndExit(c, "renew", 1) // last argument is exit code 29 | } 30 | 31 | // Renew keys from this dir. 32 | certsDir := c.String("dir") 33 | 34 | // Get email and domain. 35 | email := c.Args().Get(0) 36 | 37 | // Renew a certificate. 38 | newCertificates, err := renewCerts(certsDir, email) 39 | if err != nil { 40 | log.Fatalln(err) 41 | } 42 | 43 | // New certificate comes back with the cert bytes, the bytes of 44 | // the client's private key, and a certificate URL. This is where 45 | // you should save them to files! 46 | err = saveCerts(certsDir, newCertificates) 47 | if err != nil { 48 | log.Fatalln(err) 49 | } 50 | log.Printf("Renewed certificates for user %s under %s\n", email, certsDir) 51 | } 52 | -------------------------------------------------------------------------------- /server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "errors" 21 | "log" 22 | "net" 23 | "strings" 24 | "time" 25 | 26 | "github.com/minio/cli" 27 | ) 28 | 29 | // main for server command. 30 | func serverMain(c *cli.Context) { 31 | if !c.Args().Present() || c.Args().First() == "help" { 32 | cli.ShowCommandHelpAndExit(c, "renew", 1) // last argument is 33 | // exit code 34 | } 35 | 36 | // Renew keys from this dir. 37 | certsDir := c.String("dir") 38 | 39 | // Sub domains if any. 40 | subDomainsValue := c.String("sub-domains") 41 | subDomains := strings.Split(subDomainsValue, ",") 42 | 43 | // SAN domains if any. 44 | sanDomainsValue := c.String("san-domains") 45 | sanDomains := strings.Split(sanDomainsValue, ",") 46 | 47 | // Get email and domain. 48 | email := c.Args().Get(0) 49 | domain := c.Args().Get(1) 50 | 51 | // Validate if its a valid domain. 52 | if !isValidDomain(domain) { 53 | log.Fatalln(&net.DNSError{Err: "Invalid domain name", Name: domain}) 54 | } 55 | // Validate if domain is already a sub domain, error if 56 | // sub-domains option is specified. 57 | if isSubDomain(domain) && len(subDomainsValue) > 0 { 58 | log.Fatalln(errors.New("Domain is already a subdomain, SSL certs not allowed for sub sub domains.")) 59 | } 60 | 61 | if !isCertAvailable(certsDir) { 62 | newCertificates, err := genCerts(email, domain, subDomains, sanDomains) 63 | if err != nil { 64 | log.Fatalln(err) 65 | } 66 | // Each certificate comes back with the cert bytes, the bytes 67 | // of the client's private key, and a certificate URL. This is 68 | // where you should save them to files! 69 | err = saveCerts(certsDir, newCertificates) 70 | if err != nil { 71 | log.Fatalln(err) 72 | } 73 | expTime, err := getCertExpTime(certsDir) 74 | if err != nil { 75 | log.Fatalln(err) 76 | } 77 | log.Printf("Generated certificates for %s under %s will expire in %d days.\n", domain, certsDir, int(expTime.Sub(time.Now()).Hours()/24.0)) 78 | } 79 | log.Printf("Starting timer thread waiting for %d\n", renewDaysLimit) 80 | // Initialize a new timer, ticks every 45 days. 81 | for range time.Tick((renewDaysLimit - 1) * 24 * time.Hour) { 82 | newCertificates, err := renewCerts(certsDir, email) 83 | if err != nil { 84 | log.Fatalln(err) 85 | } 86 | // Each certificate comes back with the cert bytes, the bytes 87 | // of the client's private key, and a certificate URL. This is 88 | // where you should save them to files! 89 | err = saveCerts(certsDir, newCertificates) 90 | if err != nil { 91 | log.Fatalln(err) 92 | } 93 | expTime, err := getCertExpTime(certsDir) 94 | if err != nil { 95 | log.Fatalln(err) 96 | } 97 | log.Printf("Renewed certificates for %s under %s will expire in %d days.\n", domain, certsDir, int(expTime.Sub(time.Now()).Hours()/24.0)) 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /user.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Concert (C) 2016 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "crypto" 21 | 22 | "github.com/xenolf/lego/acme" 23 | ) 24 | 25 | // conUser - You'll need a user or account type that implements acme.User 26 | type conUser struct { 27 | Email string 28 | Registration *acme.RegistrationResource 29 | key crypto.PrivateKey 30 | } 31 | 32 | // GetEmail - 33 | func (u conUser) GetEmail() string { 34 | return u.Email 35 | } 36 | 37 | // GetRegistration - 38 | func (u conUser) GetRegistration() *acme.RegistrationResource { 39 | return u.Registration 40 | } 41 | 42 | // GetPrivateKey - 43 | func (u conUser) GetPrivateKey() crypto.PrivateKey { 44 | return u.key 45 | } 46 | --------------------------------------------------------------------------------