├── test ├── fixtures │ └── cookbooks │ │ ├── acme_client │ │ ├── files │ │ │ └── index.html │ │ ├── metadata.rb │ │ ├── attributes │ │ │ └── default.rb │ │ ├── templates │ │ │ └── nginx-test.conf │ │ └── recipes │ │ │ ├── nginx.rb │ │ │ └── http.rb │ │ └── acme_server │ │ ├── templates │ │ └── default │ │ │ └── golang.sh.erb │ │ ├── metadata.rb │ │ ├── files │ │ └── default │ │ │ └── pebble-config.json │ │ └── recipes │ │ └── default.rb └── integration │ └── http │ └── inspec │ └── certificate_spec.rb ├── .rubocop.yml ├── templates └── acme-challange.nginx.erb ├── Berksfile ├── .gitignore ├── metadata.rb ├── kitchen.yml ├── .travis.yml ├── recipes └── default.rb ├── attributes └── default.rb ├── chefignore ├── resources ├── ssl_key.rb ├── selfsigned.rb └── certificate.rb ├── libraries └── acme.rb ├── CHANGELOG.md ├── LICENSE └── README.md /test/fixtures/cookbooks/acme_client/files/index.html: -------------------------------------------------------------------------------- 1 | Hello World! 2 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | TargetChefVersion: 17.latest 3 | Chef/Modernize/FoodcriticComments: 4 | Enabled: true 5 | Chef/Style/CopyrightCommentFormat: 6 | Enabled: true 7 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_server/templates/default/golang.sh.erb: -------------------------------------------------------------------------------- 1 | export PATH="<%= ::File.join(@install_dir, 'go', 'bin') %>:<%= @gobin %>:$PATH" 2 | export GOPATH=<%= @gopath %> 3 | export GOBIN=<%= @gobin %> 4 | -------------------------------------------------------------------------------- /templates/acme-challange.nginx.erb: -------------------------------------------------------------------------------- 1 | server { 2 | listen 443 ssl; 3 | server_name <%= @host %>; 4 | ssl_certificate <%= @cert %>; 5 | ssl_certificate_key <%= @key %>; 6 | 7 | return 202; 8 | } 9 | -------------------------------------------------------------------------------- /Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | group :integration do 4 | cookbook 'acme_client', path: 'test/fixtures/cookbooks/acme_client' 5 | cookbook 'acme_server', path: 'test/fixtures/cookbooks/acme_server' 6 | end 7 | 8 | metadata 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *# 3 | .#* 4 | \#*# 5 | .*.sw[a-z] 6 | *.un~ 7 | pkg/ 8 | 9 | # Berkshelf 10 | .vagrant 11 | /cookbooks 12 | Berksfile.lock 13 | 14 | # Bundler 15 | Gemfile.lock 16 | bin/* 17 | .bundle/* 18 | 19 | .kitchen/ 20 | .kitchen.local.yml 21 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_server/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'acme_server' 2 | maintainer 'Thijs Houtenbos' 3 | maintainer_email 'thoutenbos@schubergphilis.com' 4 | license 'Apache-2.0' 5 | description 'Installs/Configures the Boulder Acme server' 6 | version '0.1.0' 7 | 8 | depends 'golang', '>= 5.3.0' 9 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_client/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'acme_client' 2 | maintainer 'Thijs Houtenbos' 3 | maintainer_email 'thoutenbos@schubergphilis.com' 4 | license 'Apache-2.0' 5 | description 'Configures the Acme cookbook' 6 | version '0.1.0' 7 | 8 | depends 'acme' 9 | depends 'nginx' 10 | depends 'hostsfile' 11 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name 'acme' 2 | maintainer 'Thijs Houtenbos' 3 | maintainer_email 'thoutenbos@schubergphilis.com' 4 | license 'Apache-2.0' 5 | description 'ACME client cookbook for free and trusted SSL/TLS certificates from Let\'s Encrypt' 6 | source_url 'https://github.com/schubergphilis/chef-acme' 7 | issues_url 'https://github.com/schubergphilis/chef-acme/issues' 8 | version '4.2.0' 9 | chef_version '>= 15.3' 10 | 11 | %w(ubuntu debian redhat centos fedora).each do |os| 12 | supports os 13 | end 14 | -------------------------------------------------------------------------------- /kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver: 3 | name: vagrant 4 | customize: 5 | memory: 1024 6 | 7 | verifier: 8 | name: inspec 9 | 10 | provisioner: 11 | name: chef_zero 12 | product_name: chef 13 | chef_license: accept-no-persist 14 | deprecations_as_errors: true 15 | 16 | platforms: 17 | - name: centos-stream-10 18 | - name: debian-13 19 | - name: ubuntu-24.04 20 | 21 | suites: 22 | - name: http 23 | run_list: 24 | - recipe[acme_server] 25 | - recipe[acme_client::http] 26 | attributes: 27 | acme: 28 | dir: https://127.0.0.1:14000/dir 29 | contact: 30 | - mailto:admin@example.com 31 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_client/attributes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme_client 4 | # Attribute:: default 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | default['nginx']['default_site_enabled'] = false 22 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_server/files/default/pebble-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "pebble": { 3 | "listenAddress": "0.0.0.0:14000", 4 | "managementListenAddress": "0.0.0.0:15000", 5 | "certificate": "test/certs/localhost/cert.pem", 6 | "privateKey": "test/certs/localhost/key.pem", 7 | "httpPort": 80, 8 | "tlsPort": 443, 9 | "ocspResponderURL": "", 10 | "externalAccountBindingRequired": false, 11 | "domainBlocklist": ["blocked-domain.example"], 12 | "retryAfter": { 13 | "authz": 3, 14 | "order": 5 15 | }, 16 | "profiles": { 17 | "default": { 18 | "description": "The profile you know and love", 19 | "validityPeriod": 7776000 20 | }, 21 | "shortlived": { 22 | "description": "A short-lived cert profile, without actual enforcement", 23 | "validityPeriod": 518400 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | dist: trusty 3 | sudo: required 4 | services: docker 5 | addons: 6 | apt: 7 | sources: 8 | - chef-stable-trusty 9 | packages: 10 | - chefdk 11 | 12 | cache: 13 | apt: true 14 | 15 | env: 16 | global: 17 | - KITCHEN_LOCAL_YAML=kitchen.dokken.yml 18 | - CHEF_LICENSE="accept-no-persist" 19 | matrix: 20 | - CMD="chef exec cookstyle --display-cop-names --extra-details" 21 | - CMD="chef exec foodcritic ." 22 | - CMD="kitchen verify centos-7" 23 | - CMD="kitchen verify ubuntu-1604" 24 | 25 | matrix: 26 | fast_finish: true 27 | allow_failures: 28 | - env: CMD="kitchen verify ubuntu-1604" 29 | 30 | before_install: 31 | - eval "$(/opt/chefdk/bin/chef shell-init bash)" 32 | - chef --version 33 | 34 | # We are using ChefDK so no gem install required. 35 | # https://docs.travis-ci.com/user/customizing-the-build#Skipping-the-Installation-Step 36 | install: true 37 | 38 | script: ${CMD} 39 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # Recipe:: default 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | if Gem::Version.new(node['acme']['gem_version']) < Gem::Version.new('2.0.30') 22 | Chef::Log.warn('acme acme-client gem version 2.0.30 or newer is required') 23 | end 24 | 25 | chef_gem 'acme-client' do 26 | action :install 27 | version node['acme']['gem_version'] 28 | compile_time true 29 | end 30 | 31 | require 'acme-client' 32 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # Attribute:: default 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | default['acme']['contact'] = [] 22 | default['acme']['dir'] = 'https://acme-v02.api.letsencrypt.org/directory' 23 | default['acme']['renew'] = 30 24 | default['acme']['source_ips'] = %w(66.133.109.36 64.78.149.164) 25 | 26 | default['acme']['private_key'] = nil 27 | default['acme']['private_key_file'] = '/etc/acme/account_private_key.pem' 28 | default['acme']['gem_version'] = '2.0.30' 29 | default['acme']['key_size'] = 2048 30 | default['acme']['ec_curve'] = 'prime256v1' 31 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_client/templates/nginx-test.conf: -------------------------------------------------------------------------------- 1 | server { 2 | 3 | listen 80 default_server; 4 | 5 | server_name test.example.com; 6 | access_log /var/log/nginx/localhost.access.log; 7 | error_log /var/log/nginx/localhost.error.log; 8 | 9 | location / { 10 | root /var/www/html; 11 | index index.html; 12 | } 13 | } 14 | 15 | 16 | server { 17 | 18 | listen 443 ssl default_server; 19 | 20 | ssl_certificate /etc/ssl/test.example.com.crt; 21 | ssl_certificate_key /etc/ssl/test.example.com.key; 22 | 23 | server_name test.example.com; 24 | access_log /var/log/nginx/localhost.access.log; 25 | error_log /var/log/nginx/localhost.error.log; 26 | 27 | location / { 28 | root /var/www/html; 29 | index index.html; 30 | } 31 | } 32 | 33 | server { 34 | 35 | listen 443 ssl; 36 | 37 | ssl_certificate /etc/ssl/ec.example.com.crt; 38 | ssl_certificate_key /etc/ssl/ec.example.com.key; 39 | 40 | 41 | ssl_protocols TLSv1.2 TLSv1.3; 42 | ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384; 43 | ssl_prefer_server_ciphers on; 44 | 45 | server_name ec.example.com; 46 | access_log /var/log/nginx/ec.access.log; 47 | error_log /var/log/nginx/ec.error.log; 48 | 49 | location / { 50 | root /var/www/html; 51 | index index.html; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_client/recipes/nginx.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme_client 4 | # Recipe:: nginx 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | # Install a webserver 22 | nginx_install 'nginx' do 23 | source 'distro' 24 | end 25 | 26 | nginx_config 'nginx' 27 | 28 | nginx_service 'nginx' do 29 | action :start 30 | end 31 | 32 | nginx_site 'test' do 33 | cookbook cookbook_name 34 | template 'nginx-test.conf' 35 | end 36 | 37 | file '/etc/nginx/conf.http.d/list.conf' do 38 | content <<-EOF 39 | # Include files 40 | include /etc/nginx/conf.http.d/default-site.conf; 41 | include /etc/nginx/conf.http.d/test.conf; 42 | EOF 43 | end 44 | 45 | service 'nginx' do 46 | action :restart 47 | end 48 | -------------------------------------------------------------------------------- /chefignore: -------------------------------------------------------------------------------- 1 | # Put files/directories that should be ignored in this file when uploading 2 | # or sharing to the community site. 3 | # Lines that start with '# ' are comments. 4 | 5 | # OS generated files # 6 | ###################### 7 | .DS_Store 8 | Icon? 9 | nohup.out 10 | ehthumbs.db 11 | Thumbs.db 12 | 13 | # SASS # 14 | ######## 15 | .sass-cache 16 | 17 | # EDITORS # 18 | ########### 19 | \#* 20 | .#* 21 | *~ 22 | *.sw[a-z] 23 | *.bak 24 | REVISION 25 | TAGS* 26 | tmtags 27 | *_flymake.* 28 | *_flymake 29 | *.tmproj 30 | .project 31 | .settings 32 | mkmf.log 33 | 34 | ## COMPILED ## 35 | ############## 36 | a.out 37 | *.o 38 | *.pyc 39 | *.so 40 | *.com 41 | *.class 42 | *.dll 43 | *.exe 44 | */rdoc/ 45 | 46 | # Testing # 47 | ########### 48 | .watchr 49 | .rspec 50 | spec/* 51 | spec/fixtures/* 52 | test/* 53 | features/* 54 | Guardfile 55 | Procfile 56 | 57 | # SCM # 58 | ####### 59 | .git 60 | */.git 61 | .gitignore 62 | .gitmodules 63 | .gitconfig 64 | .gitattributes 65 | .svn 66 | */.bzr/* 67 | */.hg/* 68 | */.svn/* 69 | 70 | # Berkshelf # 71 | ############# 72 | cookbooks/* 73 | tmp 74 | 75 | # Cookbooks # 76 | ############# 77 | CONTRIBUTING 78 | CHANGELOG* 79 | 80 | # Strainer # 81 | ############ 82 | Colanderfile 83 | Strainerfile 84 | .colander 85 | .strainer 86 | 87 | # Vagrant # 88 | ########### 89 | .vagrant 90 | Vagrantfile 91 | 92 | # Travis # 93 | ########## 94 | .travis.yml 95 | -------------------------------------------------------------------------------- /resources/ssl_key.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # Resource:: ssl_key 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | unified_mode true 22 | 23 | default_action :create_if_missing 24 | 25 | property :path, String, name_property: true 26 | property :length, Integer, default: 2048 27 | property :output_format, Symbol, equal_to: [:pem, :der, :text], default: :pem 28 | property :type, Symbol, equal_to: [:rsa, :dsa], default: :rsa 29 | 30 | def load 31 | klass = OpenSSL::PKey.const_get(type.upcase) 32 | klass.new(::File.read(path)) if ::File.exist?(path) 33 | end 34 | 35 | def do_action(file_action) 36 | klass = OpenSSL::PKey.const_get(new_resource.type.upcase) 37 | key = klass.new(new_resource.length) 38 | data = key.send("to_#{new_resource.output_format}".to_sym) 39 | 40 | file new_resource.path do 41 | owner owner 42 | group group 43 | mode '400' 44 | content data 45 | sensitive true 46 | 47 | action file_action 48 | end 49 | end 50 | 51 | action :create do 52 | do_action(:create) 53 | end 54 | 55 | action :destroy do 56 | do_action(:destroy) 57 | end 58 | 59 | action :create_if_missing do 60 | do_action(:create_if_missing) 61 | end 62 | -------------------------------------------------------------------------------- /resources/selfsigned.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # Resource:: selfsigned 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | unified_mode true 22 | 23 | default_action :create 24 | 25 | property :cn, String, name_property: true 26 | property :alt_names, Array, default: [] 27 | 28 | property :crt, [String, nil], required: true 29 | property :key, [String, nil], required: true 30 | 31 | property :chain, [String, nil] 32 | 33 | property :owner, [String, Integer], default: 'root' 34 | property :group, [String, Integer], default: 'root' 35 | 36 | property :key_size, Integer, default: lazy { node['acme']['key_size'] }, equal_to: [2048, 3072, 4096] 37 | 38 | action :create do 39 | file "#{new_resource.cn} SSL selfsigned key" do 40 | path new_resource.key 41 | owner new_resource.owner 42 | group new_resource.group 43 | mode '400' 44 | content OpenSSL::PKey::RSA.new(new_resource.key_size).to_pem 45 | sensitive true 46 | action :create_if_missing 47 | end 48 | 49 | file "#{new_resource.cn} SSL selfsigned crt" do 50 | path new_resource.crt 51 | owner new_resource.owner 52 | group new_resource.group 53 | mode '644' 54 | content lazy { self_signed_cert(new_resource.cn, new_resource.alt_names, OpenSSL::PKey::RSA.new(::File.read(new_resource.key))).to_pem } 55 | action :create_if_missing 56 | end 57 | 58 | file "#{new_resource.cn} SSL selfsigned chain" do 59 | path new_resource.chain unless new_resource.chain.nil? 60 | owner new_resource.owner 61 | group new_resource.group 62 | mode '644' 63 | content lazy { self_signed_cert(new_resource.cn, new_resource.alt_names, OpenSSL::PKey::RSA.new(::File.read(new_resource.key))).to_pem } 64 | not_if { new_resource.chain.nil? } 65 | action :create_if_missing 66 | end 67 | end 68 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_server/recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme_server 4 | # Recipe:: default 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | apt_update 'update' if platform_family?('debian') 22 | package 'selinux-utils' if platform_family?('debian') 23 | 24 | node.default['golang']['version'] = '1.24.4' 25 | 26 | platform = case node['kernel']['machine'] 27 | when /i.86/ 28 | '386' 29 | when /aarch64/ 30 | 'arm64' 31 | else 32 | 'amd64' 33 | end 34 | 35 | golang 'Install go' do 36 | scm_packages ['git'] 37 | version node['golang']['version'] if node['golang']['version'] 38 | url "https://go.dev/dl/go#{node['golang']['version']}.linux-#{platform}.tar.gz" 39 | end 40 | 41 | node['golang']['packages'].each do |package| 42 | golang_package package 43 | end 44 | 45 | git '/usr/local/src/pebble' do 46 | repository 'https://github.com/letsencrypt/pebble.git' 47 | revision 'v2.8.0' 48 | end 49 | 50 | execute '/usr/local/go/bin/go install ./cmd/pebble' do 51 | cwd '/usr/local/src/pebble' 52 | environment ({'GOPATH' => '/opt/go', 'GOBIN' => '/opt/go/bin'}) 53 | end 54 | 55 | selinux_fcontext '/opt/go/bin/pebble' do 56 | secontext 'usr_t' 57 | end 58 | 59 | cookbook_file '/usr/local/src/pebble/test/config/pebble-config.json' do 60 | source 'pebble-config.json' 61 | end 62 | 63 | # Needed for the acme-client gem to continue connecting to pebble; 64 | # please do NOT do this on production Chef nodes! 65 | execute 'update Chef trusted certificates store' do 66 | command "cat /usr/local/src/pebble/test/certs/pebble.minica.pem >> /opt/chef/embedded/ssl/certs/cacert.pem && touch /opt/chef/embedded/ssl/certs/PEBBLE-MINICA-IS-INSTALLED" 67 | creates '/opt/chef/embedded/ssl/certs/PEBBLE-MINICA-IS-INSTALLED' 68 | end 69 | 70 | user 'pebble' do 71 | system true 72 | shell '/bin/false' 73 | end 74 | 75 | systemd_unit 'pebble.service' do 76 | content <<~EOU 77 | [Unit] 78 | Description=Pebble ACME Server 79 | 80 | [Service] 81 | User=pebble 82 | WorkingDirectory=/usr/local/src/pebble 83 | ExecStart=#{node['golang']['gobin']}/pebble -config ./test/config/pebble-config.json 84 | Environment="GOPATH=#{node['golang']['gopath']}" 85 | Environment="GOBIN=#{node['golang']['gobin']}" 86 | Environment="PEBBLE_VA_ALWAYS_VALID=0" 87 | Environment="PEBBLE_VA_NOSLEEP=1" 88 | Environment="PEBBLE_WFE_NONCEREJECT=0" 89 | Environment="PEBBLE_AUTHZREUSE=0" 90 | 91 | [Install] 92 | WantedBy=multi-user.target 93 | EOU 94 | action :create 95 | end 96 | 97 | service 'pebble' do 98 | action [:enable, :start] 99 | end 100 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/acme_client/recipes/http.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme_client 4 | # Recipe:: default 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | include_recipe 'acme' 22 | 23 | hostsfile_entry '127.0.0.1' do 24 | hostname 'localhost' 25 | aliases [ 26 | 'localhost.localdomain', 27 | 'localhost4', 28 | 'localhost4.localdomain4', 29 | 'test.example.com', 30 | 'test1.example.com', 31 | 'test2.example.com', 32 | 'new.example.com', 33 | 'web.example.com', 34 | 'mail.example.com', 35 | '4096.example.com', 36 | 'ec.example.com', 37 | 'short.example.com', 38 | ] 39 | end 40 | 41 | # Generate selfsigned certificates so nginx can start 42 | %w(test new web ec).each do |n| 43 | acme_selfsigned "#{n}.example.com" do 44 | crt "/etc/ssl/#{n}.example.com.crt" 45 | key "/etc/ssl/#{n}.example.com.key" 46 | end 47 | end 48 | 49 | include_recipe 'acme_client::nginx' 50 | 51 | # Request the real certificate 52 | acme_certificate 'test.example.com' do 53 | alt_names ['test1.example.com', 'test2.example.com'] 54 | crt '/etc/ssl/test.example.com.crt' 55 | key '/etc/ssl/test.example.com.key' 56 | wwwroot '/var/www/html' 57 | notifies :reload, 'nginx_service[nginx]', :immediately 58 | end 59 | 60 | acme_certificate 'new.example.com' do 61 | crt '/etc/ssl/new.example.com.crt' 62 | key '/etc/ssl/new.example.com.key' 63 | wwwroot '/var/www/html' 64 | end 65 | 66 | acme_certificate '4096.example.com' do 67 | crt '/etc/ssl/4096.example.com.crt' 68 | key '/etc/ssl/4096.example.com.key' 69 | key_size 4096 70 | wwwroot '/var/www/html' 71 | end 72 | 73 | acme_certificate 'web.example.com' do 74 | alt_names ['mail.example.com'] 75 | crt '/etc/ssl/web.example.com.crt' 76 | key '/etc/ssl/web.example.com.key' 77 | wwwroot '/var/www/html' 78 | notifies :reload, 'nginx_service[nginx]', :immediately 79 | end 80 | 81 | # Generate selfsigned certificate with both DNS and IP SANs for test 82 | acme_selfsigned 'ip.example.com' do 83 | alt_names ['192.168.18.17'] 84 | crt '/etc/ssl/ip.example.com.crt' 85 | key '/etc/ssl/ip.example.com.key' 86 | end 87 | 88 | acme_certificate 'ec.example.com' do 89 | crt '/etc/ssl/ec.example.com.crt' 90 | key '/etc/ssl/ec.example.com.key' 91 | key_type 'ec' 92 | ec_curve 'prime256v1' 93 | wwwroot '/var/www/html' 94 | end 95 | 96 | # Request certificate with both DNS and IP SANs (requires short-lived profile) 97 | acme_certificate 'short.example.com' do 98 | alt_names [node['ipaddress']] 99 | crt '/etc/ssl/short.example.com.crt' 100 | key '/etc/ssl/short.example.com.key' 101 | wwwroot '/var/www/html' 102 | profile 'shortlived' 103 | notifies :reload, 'nginx_service[nginx]', :immediately 104 | end 105 | -------------------------------------------------------------------------------- /test/integration/http/inspec/certificate_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # 5 | # Copyright:: 2015-2021, Schuberg Philis 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | describe x509_certificate('/etc/ssl/test.example.com.crt') do 21 | it { should be_certificate } 22 | its('key_length') { should be 2048 } 23 | its('validity_in_days') { should be > 30 } 24 | its('extensions.subjectAltName') { should include 'DNS:test.example.com' } 25 | its('extensions.subjectAltName') { should include 'DNS:test1.example.com' } 26 | its('extensions.subjectAltName') { should include 'DNS:test2.example.com' } 27 | its('issuer.CN') { should match /Pebble Intermediate CA/ } 28 | end 29 | 30 | # no corresponding inspec resource 31 | # describe x509_private_key('/etc/ssl/test.example.com.key') do 32 | # it { should be_valid } 33 | # it { should_not be_encrypted } 34 | # it { should have_matching_certificate('/etc/ssl/test.example.com.crt') } 35 | # end 36 | 37 | describe command('echo | openssl s_client -connect 0:443 2>&1 | openssl x509 -noout -serial') do 38 | its('stdout') { should eq command('openssl x509 -in /etc/ssl/test.example.com.crt -noout -serial').stdout } 39 | end 40 | 41 | describe x509_certificate('/etc/ssl/new.example.com.crt') do 42 | it { should be_certificate } 43 | its('key_length') { should be 2048 } 44 | its('validity_in_days') { should be > 30 } 45 | its('extensions.subjectAltName') { should include 'DNS:new.example.com' } 46 | its('issuer.CN') { should match /Pebble Intermediate CA/ } 47 | end 48 | 49 | describe x509_certificate('/etc/ssl/4096.example.com.crt') do 50 | it { should be_certificate } 51 | its('key_length') { should be 4096 } 52 | its('validity_in_days') { should be > 30 } 53 | its('extensions.subjectAltName') { should include 'DNS:4096.example.com' } 54 | its('issuer.CN') { should match /Pebble Intermediate CA/ } 55 | end 56 | 57 | describe x509_certificate('/etc/ssl/web.example.com.crt') do 58 | it { should be_certificate } 59 | its('validity_in_days') { should be > 30 } 60 | its('extensions.subjectAltName') { should include 'DNS:web.example.com' } 61 | its('extensions.subjectAltName') { should include 'DNS:mail.example.com' } 62 | end 63 | 64 | describe x509_certificate('/etc/ssl/ip.example.com.crt') do 65 | it { should be_certificate } 66 | its('validity_in_days') { should be > 20 } 67 | its('extensions.subjectAltName') { should include 'DNS:ip.example.com' } 68 | its('extensions.subjectAltName') { should include 'IP Address:192.168.18.17' } 69 | end 70 | 71 | describe x509_certificate('/etc/ssl/ec.example.com.crt') do 72 | it { should be_certificate } 73 | its('validity_in_days') { should be > 30 } 74 | its('extensions.subjectAltName') { should include 'DNS:ec.example.com' } 75 | its('issuer.CN') { should match /Pebble Intermediate CA/ } 76 | end 77 | 78 | describe x509_certificate('/etc/ssl/short.example.com.crt') do 79 | it { should be_certificate } 80 | its('key_length') { should be 2048 } 81 | # Short-lived certificates are valid for ~6 days 82 | its('validity_in_days') { should be <= 7 } 83 | its('validity_in_days') { should be > 0 } 84 | its('extensions.subjectAltName') { should include 'DNS:short.example.com' } 85 | its('issuer.CN') { should match /Pebble Intermediate CA/ } 86 | end 87 | -------------------------------------------------------------------------------- /libraries/acme.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # Library:: acme 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | begin 22 | require 'acme-client' 23 | rescue LoadError => e 24 | Chef::Log.warn("Acme library dependency 'acme-client' not loaded: #{e}") 25 | end 26 | 27 | def names_changed?(cert, names) 28 | return false if names.empty? 29 | 30 | san_extension = cert.extensions.find { |e| e.oid == 'subjectAltName' } 31 | return false if san_extension.nil? 32 | 33 | current = san_extension.value.split(', ').map { |v| v.split(':')[1] } 34 | !(names - current).empty? || !(current - names).empty? 35 | end 36 | 37 | def format_names(names) 38 | return nil if names.nil? || names.empty? 39 | 40 | names.map do |name| 41 | if valid_ip_address?(name) 42 | { type: 'ip', value: name } 43 | else 44 | { type: 'dns', value: name } 45 | end 46 | end 47 | end 48 | 49 | def acme_client 50 | return @client if @client 51 | 52 | # load private_key from disk if present 53 | private_key_file = node['acme']['private_key_file'] 54 | node.default['acme']['private_key'] = ::File.read(private_key_file) if ::File.exist?(private_key_file) 55 | 56 | private_key = OpenSSL::PKey::RSA.new(node['acme']['private_key'] || 2048) 57 | 58 | directory = new_resource.dir || node['acme']['dir'] 59 | 60 | contact = (new_resource.contact.nil? || new_resource.contact.empty?) ? node['acme']['contact'] : new_resource.contact 61 | 62 | @client = Acme::Client.new(private_key: private_key, directory: directory) 63 | 64 | if node['acme']['private_key'].nil? 65 | acme_client.new_account(contact: contact, terms_of_service_agreed: true) 66 | node.default['acme']['private_key'] = private_key.to_pem 67 | 68 | # write key to disk for persistence 69 | directory File.dirname(private_key_file) do 70 | recursive true 71 | end 72 | 73 | file private_key_file do 74 | content private_key.to_pem 75 | mode '600' 76 | sensitive true 77 | end 78 | end 79 | 80 | @client 81 | end 82 | 83 | def acme_order_certs_for(names, profile: nil) 84 | order_params = { identifiers: names } 85 | order_params[:profile] = profile if profile 86 | acme_client.new_order(**order_params) 87 | end 88 | 89 | def acme_validate(authz) 90 | authz.request_validation 91 | 92 | times = 60 93 | 94 | while times > 0 95 | break unless ( authz.status == 'pending' || authz.status == 'processing' ) 96 | times -= 1 97 | sleep 1 98 | authz.reload 99 | end 100 | 101 | authz 102 | end 103 | 104 | def acme_cert(order, cn, key, alt_names = []) 105 | csr = Acme::Client::CertificateRequest.new( 106 | common_name: cn, 107 | names: alt_names, 108 | private_key: key 109 | ) 110 | order.finalize(csr: csr) 111 | 112 | while order.status == 'processing' 113 | sleep 1 114 | order.reload 115 | end 116 | 117 | order.certificate 118 | end 119 | 120 | def create_subject_alt_names(alt_names) 121 | return nil if alt_names.nil? || alt_names.empty? 122 | 123 | alt_names.map do |name| 124 | if valid_ip_address?(name) 125 | "IP:#{name}" 126 | else 127 | "DNS:#{name}" 128 | end 129 | end.join(',') 130 | end 131 | 132 | def valid_ip_address?(address) 133 | require 'ipaddr' 134 | begin 135 | ip = IPAddr.new(address) 136 | true 137 | rescue IPAddr::InvalidAddressError, IPAddr::AddressFamilyError 138 | false 139 | end 140 | end 141 | 142 | def self_signed_cert(cn, alts, key) 143 | cert = OpenSSL::X509::Certificate.new 144 | cert.subject = cert.issuer = OpenSSL::X509::Name.new([['CN', cn, OpenSSL::ASN1::UTF8STRING]]) 145 | cert.not_before = Time.now 146 | cert.not_after = Time.now + 60 * 60 * 24 * node['acme']['renew'] 147 | cert.public_key = key.public_key 148 | cert.serial = 0x0 149 | cert.version = 2 150 | 151 | ef = OpenSSL::X509::ExtensionFactory.new 152 | ef.subject_certificate = cert 153 | ef.issuer_certificate = cert 154 | 155 | cert.extensions = [] 156 | 157 | cert.extensions += [ef.create_extension('basicConstraints', 'CA:FALSE', true)] 158 | cert.extensions += [ef.create_extension('subjectKeyIdentifier', 'hash')] 159 | san = create_subject_alt_names([cn] + alts) 160 | cert.extensions += [ef.create_extension('subjectAltName', san)] if san 161 | 162 | cert.sign key, OpenSSL::Digest.new('SHA256') 163 | end 164 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ACME Cookbook Changelog 2 | ============== 3 | 4 | This file is used to list changes made in each version of the acme cookbook. 5 | 6 | 4.2.0 7 | ---------- 8 | - stanhu - Add support for short-lived IP certificates 9 | - thoutenbos - Update test kitchen 10 | - thoutenbos - Upgrade acme-client gem to v2.0.30 11 | 12 | 4.1.8 13 | ---------- 14 | - thoutenbos - Update Test Kitchen 15 | - thoutenbos - Upgrade acme-client gem to v2.0.24 16 | - hristiy4n - Add support for ECC keys 17 | 18 | 4.1.7 19 | ---------- 20 | - seansith - Upgrade acme-client gem to v2.0.15 21 | - stanhu - Make SAN entries support IP addresses 22 | 23 | 4.1.6 24 | ---------- 25 | - ramereth - Allow `Integer` for `owner` and `group` properties 26 | - bugoff - Add processing as a valid authz status 27 | - thoutenbos - Update Test Kitchen 28 | - thoutenbos - Upgrade acme-client gem to v2.0.13 29 | 30 | 4.1.5 31 | ---------- 32 | - twk3 - make private key file location configurable 33 | - thoutenbos - Upgrade acme-client gem to v2.0.9 34 | 35 | 4.1.4 36 | ---------- 37 | - detjensrobert - Chef 17 Compatibility 38 | - Enable `unified_mode` for all resources 39 | - Cookstyle fixes 40 | - Update test infra to use InSpec instead of legacy ServerSpec 41 | - Update test cookbook to use latest cookbooks / resources 42 | - Use standard location for Kitchen files according to [upstream](https://kitchen.ci/docs/getting-started/kitchen-yml/) 43 | - Update `acme_client` gem to Ruby 3-compliant version 44 | 45 | 4.1.3 46 | ----- 47 | - essjayhch - Improve authz failure logging 48 | - redream - Typo fix 49 | - schrd - Add DNS validation support 50 | 51 | 4.1.2 52 | ----- 53 | - zedtux - upgrade acme-client version to 2.0.6 54 | 55 | 4.1.1 56 | ----- 57 | - petracvv - lazy evaluation in resource attributes 58 | 59 | 4.1.0 60 | ----- 61 | - hrak - Ease version constraints on supported platforms 62 | - zakame - Rename `endpoint` attribute to `dir` 63 | - zakame - Remove `chain` and `fullchain` properties 64 | - zakame - Switch to `pebble` for integration testing 65 | - zakame - Implement ACME v2 support 66 | - Dawnflash - Clean up token files after use 67 | - bby-bishopclark - Various trivial English fixes in README 68 | - rmoriz - bump acme-client gem to 2.0.3 69 | - SeanSith - Marked fullchain as a deprecated_property_alias 70 | - zakame - Fixes for the Travis and kitchen tests 71 | 72 | 4.0.0 73 | ----- 74 | The TLS-SNI-01 validation method has been removed as it is no longer supported by Let's Encrypt. 75 | 76 | - borgified - Documentation fix 77 | - ibaum - Override endpoint from provider 78 | - jeffbyrnes - Removed support for TLS-SNI-01 validation 79 | - jeffbyrnes - Refactor LWRPs into Custom Resources 80 | - jeffbyrnes - Foodcritic, cookstyle and build improvements 81 | - thoutenbos - Upgrade acme-client gem to v0.6.2 82 | 83 | 3.1.0 84 | ----- 85 | - axos88 - Add ssl validation method 86 | - notapatch - Update README 87 | - funzoneq - Extra validation server IP 88 | - faucct - Remove unknown attribute validation 'min' 89 | - faucct - Make sure nginx reloads 90 | - wndhydrnt - Update certificate when common name / alternate names change 91 | 92 | 3.0.0 93 | ----- 94 | By changes in Chef 13, the unused property `method` has been removed from the `certificate` provider. 95 | 96 | - mattrobenolt - Do not allow a crt without an accompanying chain 97 | - alex-tan - Improve README 98 | - rmoriz - Allow setting custom key size 99 | - szymonpk - Add missing matchers 100 | - rmoriz - Chef 13 compatibility 101 | - rmoriz - Multiple CI build improvements 102 | 103 | 2.0.0 104 | ----- 105 | - thoutenbos - Rename from `letsencrypt` to `acme` to comply with the ISRG trademark policy 106 | - arr-dev - Add ChefSpec matchers 107 | 108 | 1.0.3 109 | ----- 110 | - chr4 - Bump versions of json-jwt and acme-client 111 | - thoutenbos - Upgrade acme-client to drop dependencies 112 | 113 | 1.0.2 114 | ----- 115 | - miguelaferreira - Wrap test cookbooks in :integration group 116 | 117 | 1.0.1 118 | ----- 119 | - thoutenbos - Work around gem dependency problems 120 | - thoutenbos - Rubocop fixes 121 | - thoutenbos - Improve the example 122 | 123 | 1.0.0 124 | ----- 125 | - seccubus - Make production the default end-point 126 | - seccubus - Add apache2 example 127 | - thoutenbos - Fix for chef-client v11 compatibility 128 | - thoutenbos - Fix integration tests 129 | 130 | 0.1.7 131 | ----- 132 | - glaszig - Use chef api inside ruby_block 133 | - arr-dev - Document `node['letsencrypt']['private_key']` 134 | 135 | 0.1.6 136 | ----- 137 | - funzoneq - Add verification IP for firewalling purposes 138 | - acoulton - fail chef run if certificate not issued, unless `ignore_failure` resource attribute set 139 | 140 | 0.1.5 141 | ----- 142 | - thoutenbos - fix selfsigned chain 143 | 144 | 0.1.4 145 | ----- 146 | - patcon - spin-off the boulder test cookbook 147 | - patcon - add Ubuntu support 148 | - thoutenbos - various improvements 149 | 150 | 0.1.3 151 | ----- 152 | - sawanoboly - Add SAN support 153 | 154 | 0.1.2 155 | ----- 156 | - obazoud - Improved logging 157 | - thoutenbos - Add Kitchen CI 158 | - thoutenbos - Fix key/cert creation order issue 159 | 160 | 0.1.1 161 | ----- 162 | - Thijs Houtenbos - Added `chain` and `fullchain` properties 163 | 164 | 0.1.0 165 | ----- 166 | - Thijs Houtenbos - Initial release 167 | 168 | - - - 169 | Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax) for help with Markdown. 170 | 171 | The [Github Flavored Markdown page](http://github.github.com/github-flavored-markdown/) describes the differences between markdown on github and standard markdown. 172 | -------------------------------------------------------------------------------- /resources/certificate.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thijs Houtenbos 3 | # Cookbook:: acme 4 | # Resource:: certificate 5 | # 6 | # Copyright:: 2015-2021, Schuberg Philis 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | unified_mode true 22 | 23 | default_action :create 24 | 25 | property :cn, String, name_property: true 26 | property :alt_names, Array, default: [] 27 | 28 | property :crt, [String, nil], required: true 29 | property :key, [String, nil], required: true 30 | 31 | property :owner, [String, Integer], default: 'root' 32 | property :group, [String, Integer], default: 'root' 33 | 34 | property :wwwroot, String, default: '/var/www' 35 | 36 | property :key_size, Integer, default: lazy { node['acme']['key_size'] }, equal_to: [2048, 3072, 4096] 37 | property :key_type, String, default: 'rsa', equal_to: %w(rsa ec) 38 | property :ec_curve, String, default: lazy { node['acme']['ec_curve'] }, equal_to: %w(prime256v1 secp384r1 secp521r1) 39 | 40 | property :profile, String, default: 'default', equal_to: ['default', 'shortlived'] 41 | 42 | property :dir, [String, nil] 43 | property :contact, Array, default: [] 44 | 45 | # if you want to use DNS authentication, you can pass the code to install and 46 | # remove the challenge as a block 47 | # 48 | # the install_authz_block will be called for each authorization with the 49 | # authorization and resource as parameter. It must return the authz object from 50 | # the authorization. 51 | # The resource will then call the acme verification process. After verification 52 | # the remove_authz_block will be called with the authz as parameter. This is 53 | # intended to allow cleanup of the challenge 54 | property :install_authz_block, [Proc, nil] 55 | property :remove_authz_block, [Proc, nil] 56 | 57 | property :chain, String, deprecated: 'The chain property has been deprecated as the acme-client gem now returns the full certificate chain by default (on the crt property.) Please update your cookbooks to remove this property.' 58 | deprecated_property_alias 'fullchain', 'crt', 'The fullchain property has been deprecated as the acme-client gem now returns the full certificate chain by default (on the crt property.) Please update your cookbooks to switch to \'crt\'.' 59 | 60 | deprecated_property_alias 'endpoint', 'dir', 'The endpoint property was renamed to dir, to reflect ACME v2 changes. Please update your cookbooks to use the new property name.' 61 | 62 | action :create do 63 | file "#{new_resource.cn} SSL key" do 64 | path new_resource.key 65 | owner new_resource.owner 66 | group new_resource.group 67 | mode '400' 68 | content case new_resource.key_type 69 | when 'rsa' 70 | OpenSSL::PKey::RSA.new(new_resource.key_size).to_pem 71 | when 'ec' 72 | OpenSSL::PKey::EC.generate(new_resource.ec_curve).to_pem 73 | end 74 | sensitive true 75 | action :nothing 76 | end.run_action(:create_if_missing) 77 | 78 | mycert = nil 79 | mykey = OpenSSL::PKey.read ::File.read new_resource.key 80 | names = [new_resource.cn, new_resource.alt_names].flatten.compact 81 | # Calculate renewal time based on profile 82 | renew_days = new_resource.profile == 'shortlived' ? 2 : node['acme']['renew'] 83 | renew_at = ::Time.now + 60 * 60 * 24 * renew_days 84 | 85 | if !new_resource.crt.nil? && ::File.exist?(new_resource.crt) 86 | mycert = ::OpenSSL::X509::Certificate.new ::File.read new_resource.crt 87 | end 88 | 89 | if mycert.nil? || mycert.not_after <= renew_at || names_changed?(mycert, names) 90 | order = acme_order_certs_for(format_names(names), profile: new_resource.profile) 91 | all_validations = [] 92 | if new_resource.install_authz_block.nil? 93 | order.authorizations.each do |authorization| 94 | authz = install_http_validation(authorization, new_resource) 95 | acme_validate(authz) 96 | remove_http_validation(authz, new_resource) 97 | all_validations.push(authz) 98 | end 99 | else 100 | ruby_block 'install and validate challenges using custom method' do 101 | block do 102 | order.authorizations.each do |authorization| 103 | authz, fqdn = new_resource.install_authz_block.call(authorization, new_resource) 104 | acme_validate(authz) 105 | new_resource.remove_authz_block.call(authz, fqdn) 106 | end 107 | end 108 | end 109 | end 110 | 111 | ruby_block "create certificate for #{new_resource.cn}" do 112 | block do 113 | unless (all_validations.map { |authz| authz.status == 'valid' }).all? 114 | errors = all_validations.select { |authz| authz.status != 'valid' }.map do |authz| 115 | "{url: #{authz.url}, status: #{authz.status}, error: #{authz.error}} " 116 | end.reduce(:+) 117 | 118 | raise "[#{new_resource.cn}] Validation failed, unable to request certificate, Errors: [#{errors}]" 119 | end 120 | 121 | begin 122 | newcert = acme_cert(order, new_resource.cn, mykey, new_resource.alt_names) 123 | rescue Acme::Client::Error => e 124 | raise "[#{new_resource.cn}] Certificate request failed: #{e.message}" 125 | else 126 | Chef::Resource::File.new("#{new_resource.cn} SSL new crt", run_context).tap do |f| 127 | f.path new_resource.crt 128 | f.owner new_resource.owner 129 | f.group new_resource.group 130 | f.content newcert 131 | f.mode 00644 132 | end.run_action :create 133 | end 134 | end 135 | end 136 | end 137 | end 138 | 139 | action_class.class_eval do 140 | def install_http_validation(authorization, new_resource) 141 | authz = authorization.http 142 | tokenpath = "#{new_resource.wwwroot}/#{authz.filename}" 143 | 144 | directory ::File.dirname(tokenpath) do 145 | owner new_resource.owner 146 | group new_resource.group 147 | mode '755' 148 | recursive true 149 | action :nothing 150 | end.run_action(:create) 151 | 152 | file tokenpath do 153 | owner new_resource.owner 154 | group new_resource.group 155 | mode '644' 156 | content authz.file_content 157 | action :nothing 158 | end.run_action(:create) 159 | authz 160 | end 161 | 162 | def remove_http_validation(authz, new_resource) 163 | tokenpath = "#{new_resource.wwwroot}/#{authz.filename}" 164 | file tokenpath do 165 | backup false 166 | action :delete 167 | end 168 | end 169 | end 170 | -------------------------------------------------------------------------------- /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 2015-2018 Schuberg Philis 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 | ACME cookbook 2 | ============= 3 | 4 | [![Build Status](https://travis-ci.org/schubergphilis/chef-acme.svg)](https://travis-ci.org/schubergphilis/chef-acme) 5 | [![Cookbook Version](https://img.shields.io/cookbook/v/acme.svg)](https://supermarket.chef.io/cookbooks/acme) 6 | 7 | Automatically get/renew free and trusted certificates from Let's Encrypt (letsencrypt.org). 8 | ACME is the [Automated Certificate Management Environment protocol][1] used by [Let's Encrypt][2]. 9 | 10 | ``` 11 | Starting with v4.0.0 of the acme cookbook the acme_ssl_certificate provider has been removed! The TLS-SNI-01 validation method used by this provider been disabled by Let's Encrypt due to security concerns. Please switch to the acme_certificate provider in this cookbook to request and renew your certificate using the supported HTTP-01 validation method. 12 | ``` 13 | 14 | Attributes 15 | ---------- 16 | 17 | | Attribute | Description | Default | 18 | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------: | 19 | | contact | Contact information, default empty. Set to `mailto:your@email.com` | [] | 20 | | dir | ACME server endpoint, Set to `https://acme-staging-v02.api.letsencrypt.org/directory` if you want to use the Let's Encrypt staging environment and corresponding certificates. | `https://acme-v02.api.letsencrypt.org/directory` | 21 | | renew | Days before the certificate expires at which the certificate will be renewed | 30 | 22 | | source_ips | IP addresses used by Let's Encrypt to verify the TLS certificates, it will change over time. This attribute is for firewall purposes. Allow these IPs for HTTP (tcp/80). | ['66.133.109.36'] | 23 | | private_key | Private key content of registered account. Private keys identify the ACME client with the endpoint and are not transferable between staging and production endpoints. | nil | 24 | | private_key_file | Filename where private key will be saved. If this file exists, the contents take precedence over the value set in `private_key`. | `/etc/acme/account_private_key.pem` | 25 | | key_size | Default private key size used when resource property is not. Must be one out of: 2048, 3072, 4096. | 2048 | 26 | 27 | 28 | Recipes 29 | ------- 30 | ### default 31 | Installs the required acme-client rubygem. 32 | 33 | Usage 34 | ----- 35 | Use the `acme_certificate` resource to request a certificate with the http-01 challenge. The webserver for the domain for which you are requesting a certificate must be running on the local server. This resource only supports the http validation method. To use the tls-sni-01 challenge, please see the resource below. Provide the path to your `wwwroot` for the specified domain. 36 | 37 | ```ruby 38 | acme_certificate 'test.example.com' do 39 | crt '/etc/ssl/test.example.com.crt' 40 | key '/etc/ssl/test.example.com.key' 41 | wwwroot '/var/www' 42 | end 43 | ``` 44 | 45 | If your webserver needs an existing certificate already when installing a new server, you will have a bootstrap problem: The web server cannot start without a certificate, but the certificate cannot be requested without the running web server. To overcome this, a temporary self-signed certificate can be generated with the `acme_selfsigned` resource, allowing the web server to start. 46 | 47 | ```ruby 48 | acme_selfsigned 'test.example.com' do 49 | crt '/etc/ssl/test.example.com.crt' 50 | chain '/etc/ssl/test.example.com-chain.crt' 51 | key '/etc/ssl/test.example.com.key' 52 | end 53 | ``` 54 | 55 | 56 | A working example can be found in the included `acme_client` test cookbook. 57 | 58 | Providers 59 | --------- 60 | ### certificate 61 | | Property | Type | Default | Description | 62 | | --- | --- | --- | --- | 63 | | `cn` | string | _name_ | The common name for the certificate | 64 | | `alt_names` | array | [] | The common name for the certificate | 65 | | `crt` | string | nil | File path to place the certificate | 66 | | `key` | string | nil | File path to place the private key | 67 | | `key_size` | integer | 2048 | Private key size. Must be one out of: 2048, 3072, 4096 | 68 | | `key_type` | string | rsa | The type of the private key. Can be `rsa` or `ec`. | 69 | | `ec_curve` | string | prime256v1 | The EC curve to use. Must be one of `prime256v1`, `secp384r1`, `secp521r1`. | 70 | | `owner` | string,integer | root | Owner of the created files | 71 | | `group` | string,integer | root | Group of the created files | 72 | | `wwwroot` | string | /var/www | Path to the wwwroot of the domain | 73 | | `ignore_failure` | boolean | false | Whether to continue chef run if issuance fails | 74 | | `retries` | integer | 0 | Number of times to catch exceptions and retry | 75 | | `retry_delay` | integer | 2 | Number of seconds to wait between retries | 76 | | `endpoint` | string | nil | The Let's Encrypt endpoint to use | 77 | | `contact` | array | [] | The contact to use | 78 | 79 | ### selfsigned 80 | | Property | Type | Default | Description | 81 | | --- | --- | --- | --- | 82 | | `cn` | string | _name_ | The common name for the certificate | 83 | | `crt` | string | nil | File path to place the certificate | 84 | | `key` | string | nil | File path to place the private key | 85 | | `key_size` | integer | 2048 | Private key size. Must be one out of: 2048, 3072, 4096 | 86 | | `chain` | string | nil | File path to place the certificate chain | 87 | | `owner` | string,integer | root | Owner of the created files | 88 | | `group` | string,integer | root | Group of the created files | 89 | 90 | Example 91 | ------- 92 | To generate a certificate for an apache2 website you can use code like this: 93 | 94 | ```ruby 95 | # Include the recipe to install the gems 96 | include_recipe 'acme' 97 | 98 | # Set up contact information. Note the mailto: notation 99 | node.override['acme']['contact'] = ['mailto:me@example.com'] 100 | # Real certificates please... 101 | node.override['acme']['endpoint'] = 'https://acme-v01.api.letsencrypt.org' 102 | 103 | site = "example.com" 104 | sans = ["www.#{site}"] 105 | 106 | # Generate a self-signed if we don't have a cert to prevent bootstrap problems 107 | acme_selfsigned "#{site}" do 108 | crt "/etc/httpd/ssl/#{site}.crt" 109 | key "/etc/httpd/ssl/#{site}.key" 110 | chain "/etc/httpd/ssl/#{site}.pem" 111 | owner "apache" 112 | group "apache" 113 | notifies :restart, "service[apache2]", :immediate 114 | end 115 | 116 | # Set up your web server here... 117 | 118 | # Get and auto-renew the certificate from Let's Encrypt 119 | acme_certificate "#{site}" do 120 | crt "/etc/httpd/ssl/#{site}.crt" 121 | key "/etc/httpd/ssl/#{site}.key" 122 | wwwroot "/var/www/#{site}/htdocs/" 123 | notifies :restart, "service[apache2]" 124 | alt_names sans 125 | end 126 | ``` 127 | 128 | DNS verification 129 | ---------------- 130 | 131 | Letsencrypt supports DNS validation. Depending on the setup there may be different ways to deploy an acme challenge to your infrastructure. If you want to use DSN validation, you have to provide two block arguments to the `acme_certificate` resource. 132 | 133 | Implement 2 methods in a library in your cookbook, each returning a `Proc` object. The following example uses a HTTP API to provide challenges to the DNS infrastructure. 134 | 135 | ```ruby 136 | # my_cookbook/libraries/acme_dns.rb 137 | 138 | class Chef 139 | class Recipe 140 | def install_dns_challenge(apitoken) 141 | Proc.new do |authorization, new_resource| 142 | # use DNS authorization 143 | authz = authorization.dns 144 | fqdn = authorization.identifier['value'] 145 | r = Net::HTTP.post(URI("https://my_awesome_dns_api/#{fqdn}"), authz.record_content, {'Authorization' => "Token #{apitoken}"}) 146 | if r.code != '200' 147 | fail "DNS API does not want to install Challenge for #{fqdn}" 148 | else 149 | # do some validation that the challenge has propagated to the infrastructure 150 | end 151 | # it is important that the authz and fqdn is passed back, so it can be passed to the remove_dns_challenge method 152 | [authz, fqdn] 153 | end 154 | end 155 | def remove_dns_challenge(apitoken) 156 | Proc.new do |authz, fqdn| 157 | uri = URI("https://my_awesome_dns_api/#{fqdn}") 158 | Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme=='https') do |http| 159 | http.delete(uri, {'Authorization' => "Token #{apitoken}"}) 160 | end 161 | end 162 | end 163 | end 164 | end 165 | ``` 166 | 167 | Use it in your recipe the following way: 168 | 169 | ```ruby 170 | apitoken = chef_vault_item(vault, item)['dns_api_token'] 171 | acme_certificate node['fqdn'] do 172 | key '/path/to/key' 173 | crt '/path/to/crt' 174 | install_authz_block install_dns_challenge(apitoken) 175 | remove_authz_block remove_dns_challenge(apitoken) 176 | end 177 | ``` 178 | 179 | 180 | 181 | Testing 182 | ------- 183 | The kitchen includes a `pebble` server to run the integration tests with, so testing can run locally without interaction with the online APIs. 184 | 185 | Contributing 186 | ------------ 187 | 1. Fork the repository on Github 188 | 2. Create a named feature branch (like `add_component_x`) 189 | 3. Write your change 190 | 4. Write tests for your change (if applicable) 191 | 5. Run the tests, ensuring they all pass 192 | 6. Submit a Pull Request using Github 193 | 194 | License and Authors 195 | ------------------- 196 | Authors: Thijs Houtenbos 197 | 198 | Credits 199 | ------- 200 | Let’s Encrypt is a trademark of the Internet Security Research Group. All rights reserved. 201 | 202 | [1]: https://ietf-wg-acme.github.io/acme/ 203 | [2]: https://letsencrypt.org/ 204 | --------------------------------------------------------------------------------