acmesmith-google-cloud-storage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f75b4b821a87ef27b6f518fd08dd893dfaeb37c6
4
+ data.tar.gz: 0df9612218e5379c3818a643355d89e3e4eb2fb4
5
+ SHA512:
6
+ metadata.gz: 71a2f9904d29d58f0a61ae921b042139600700aeeae721a3b15b022f70ac48c31e6402d5820742c452576b30adb459dc4c7a4c283e34d4d73b83d60070bbd0e8
7
+ data.tar.gz: 7968535f05faeafef1df9160dac5a7769ceb835823bc2d428e56421b6830a53655d743068875ced81b03915be400de2dbe7b32ecce5bbe08e2714862c47bcfa0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acmesmith-google-cloud-storage.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 YAMADA Tsuyoshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # acmesmith-google-cloud-storage
2
+
3
+ This gem is a plugin for [Acmesmith](https://github.com/sorah/acmesmith) and implements storage using [Google Cloud Storage](https://cloud.google.com/storage/)
4
+
5
+ ## Usage
6
+
7
+ ### Prerequisites
8
+
9
+ * You need to have service account of Google Cloud Platform to operate Google Cloud Storage via API.
10
+
11
+ ### Installation
12
+
13
+ Install `acmesith-google-cloud-storage` gem along with `acmesmith`. You can just do `gem install acmesith-google-cloud-storage` or use Bundler if you want.
14
+
15
+ ### Configuration
16
+
17
+ Use `google_cloud_storage` storage in your acmesmith.yml. General instructions about acmesmith.yml is available in the manual of Acmesmith.
18
+
19
+ ```yaml
20
+ endpoint: https://acme-staging.api.letsencrypt.org/
21
+ # endpoint: https://acme-v01.api.letsencrypt.org/ # productilon
22
+
23
+ storage:
24
+ type: google_cloud_storage
25
+ bucket:
26
+ prefix:
27
+ compute_engine_service_account: true # (pick-one): You can use GCE VM instance scope
28
+ private_key_json_file: /path/to/credential.json # (pick-one) Only JSON key file is supported
29
+
30
+ challenge_responders:
31
+ # configure how to respond ACME challenges; see the manual of Acmesmith.
32
+ ```
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
37
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acmesmith-google-cloud-storage/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acmesmith-google-cloud-storage"
8
+ spec.version = AcmesmithGoogleCloudStorage::VERSION
9
+ spec.authors = ["YAMADA Tsuyoshi"]
10
+ spec.email = ["tyamada@minimum2scp.org"]
11
+
12
+ spec.summary = %q{acmesmith plugin implementing google_cloud_storage storage}
13
+ spec.description = %q{acmesmith plugin implementing google_cloud_storage storage}
14
+ spec.homepage = "https://github.com/minimum2scp/acmesmith-google-cloud-storage"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "acmesmith"
23
+ spec.add_dependency "google-api-client", "~> 0.9.1"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acmesmith/google/cloud/storage"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module AcmesmithGoogleCloudStorage
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,192 @@
1
+ require 'acmesmith/storages/base'
2
+ require 'acmesmith/account_key'
3
+ require 'acmesmith/certificate'
4
+
5
+ require 'google/apis/storage_v1'
6
+ require 'open-uri'
7
+ require 'stringio'
8
+
9
+ module Acmesmith
10
+ module Storages
11
+ class GoogleCloudStorage < Base
12
+ attr_reader :bucket, :prefix, :compute_engine_service_account, :private_key_json_file
13
+
14
+ def initialize(bucket:, prefix:, compute_engine_service_account:nil, private_key_json_file:nil)
15
+ @bucket = bucket
16
+ @prefix = prefix
17
+ if @prefix && !@prefix.end_with?('/')
18
+ @prefix += '/'
19
+ end
20
+ @compute_engine_service_account = compute_engine_service_account
21
+ @private_key_json_file = private_key_json_file
22
+
23
+ @scope = 'https://www.googleapis.com/auth/devstorage.read_write'
24
+ @api = Google::Apis::StorageV1::StorageService.new
25
+ if @compute_engine_service_account
26
+ @api.authorization = Google::Auth.get_application_default(@scope)
27
+ elsif @private_key_json_file
28
+ credential = load_json_key(@private_key_json_file)
29
+ @api.authorization = Signet::OAuth2::Client.new(
30
+ token_credential_uri: "https://accounts.google.com/o/oauth2/token",
31
+ audience: "https://accounts.google.com/o/oauth2/token",
32
+ scope: @scope,
33
+ issuer: credential[:email_address],
34
+ signing_key: credential[:private_key])
35
+ else
36
+ raise "You need to specify authentication options (compute_engine_service_account or private_key_json_file)"
37
+ end
38
+ @api.authorization.fetch_access_token!
39
+ end
40
+
41
+ def get_account_key
42
+ obj = @api.get_object(bucket, account_key_key)
43
+ media = get_media(obj.media_link)
44
+ AccountKey.new media
45
+ rescue Google::Apis::ClientError => e
46
+ if e.status_code == 404
47
+ raise NotExist.new("Account key doesn't exist")
48
+ else
49
+ raise e
50
+ end
51
+ end
52
+
53
+ def account_key_exist?
54
+ begin
55
+ get_account_key
56
+ rescue NotExist
57
+ return false
58
+ else
59
+ return true
60
+ end
61
+ end
62
+
63
+ def put_account_key(key, passphrase = nil)
64
+ raise AlreadyExist if account_key_exist?
65
+ obj = Google::Apis::StorageV1::Object.new(
66
+ name: account_key_key,
67
+ content_type: 'application/x-pem-file'
68
+ )
69
+ @api.insert_object(bucket, obj, upload_source: StringIO.new(key.export(passphrase)))
70
+ end
71
+
72
+ def put_certificate(cert, passphrase = nil, update_current: true)
73
+ h = cert.export(passphrase)
74
+
75
+ put = -> (key, body) do
76
+ obj = Google::Apis::StorageV1::Object.new(
77
+ name: key,
78
+ content_type: 'application/x-pem-file',
79
+ )
80
+ @api.insert_object(bucket, obj, upload_source: StringIO.new(body))
81
+ end
82
+
83
+ put.call certificate_key(cert.common_name, cert.version), "#{h[:certificate].rstrip}\n"
84
+ put.call chain_key(cert.common_name, cert.version), "#{h[:chain].rstrip}\n"
85
+ put.call fullchain_key(cert.common_name, cert.version), "#{h[:fullchain].rstrip}\n"
86
+ put.call private_key_key(cert.common_name, cert.version), "#{h[:private_key].rstrip}\n"
87
+
88
+ if update_current
89
+ @api.insert_object(
90
+ bucket,
91
+ Google::Apis::StorageV1::Object.new(name: certificate_current_key(cert.common_name), content_type: 'text/plain'),
92
+ upload_source: StringIO.new(cert.version),
93
+ )
94
+ end
95
+ end
96
+
97
+ def get_certificate(common_name, version: 'current')
98
+ version = certificate_current(common_name) if version == 'current'
99
+
100
+ certificate = get_media(@api.get_object(bucket, certificate_key(common_name, version)).media_link)
101
+ chain = get_media(@api.get_object(bucket, chain_key(common_name, version)).media_link)
102
+ private_key = get_media(@api.get_object(bucket, private_key_key(common_name, version)).media_link)
103
+ Certificate.new(certificate, chain, private_key)
104
+ rescue Google::Apis::ClientError => e
105
+ if e.status_code == 404
106
+ raise NotExist.new("Certificate for #{common_name.inspect} of #{version} version doesn't exist")
107
+ else
108
+ raise e
109
+ end
110
+ end
111
+
112
+ def list_certificates
113
+ certs_prefix = "#{prefix}certs/"
114
+ objects = @api.fetch_all do |token, s|
115
+ s.list_objects(bucket, prefix: certs_prefix, page_token: token)
116
+ end
117
+ objects.map{ |obj|
118
+ regexp = /\A#{Regexp.escape(certs_prefix)}/
119
+ obj.name.sub(regexp, '').sub(/\/.+\z/, '').sub(/\/\z/, '')
120
+ }.uniq
121
+ end
122
+
123
+ def list_certificate_versions(common_name)
124
+ cert_ver_prefix = "#{prefix}certs/#{common_name}/"
125
+ objects = @api.fetch_all do |token, s|
126
+ s.list_objects(bucket, prefix: cert_ver_prefix, page_token: token)
127
+ end
128
+ objects.map { |obj|
129
+ regexp = /\A#{Regexp.escape(cert_ver_prefix)}/
130
+ obj.name.sub(regexp, '').sub(/\/.+\z/, '').sub(/\/\z/, '')
131
+ }.uniq.reject { |_| _ == 'current' }
132
+ end
133
+
134
+ def get_current_certificate_version(common_name)
135
+ certificate_current(common_name)
136
+ end
137
+
138
+ private
139
+
140
+ def account_key_key
141
+ "#{prefix}account.pem"
142
+ end
143
+
144
+ def certificate_base_key(cn, ver)
145
+ "#{prefix}certs/#{cn}/#{ver}"
146
+ end
147
+
148
+ def certificate_current_key(cn)
149
+ certificate_base_key(cn, 'current')
150
+ end
151
+
152
+ def certificate_current(cn)
153
+ obj = @api.get_object(bucket, certificate_current_key(cn))
154
+ get_media(obj.media_link).chomp
155
+ rescue Google::Apis::ClientError => e
156
+ if e.status_code == 404
157
+ raise NotExist.new("Certificate for #{cn.inspect} of current version doesn't exist")
158
+ else
159
+ raise e
160
+ end
161
+ end
162
+
163
+ def certificate_key(cn, ver)
164
+ "#{certificate_base_key(cn, ver)}/cert.pem"
165
+ end
166
+
167
+ def private_key_key(cn, ver)
168
+ "#{certificate_base_key(cn, ver)}/key.pem"
169
+ end
170
+
171
+ def chain_key(cn, ver)
172
+ "#{certificate_base_key(cn, ver)}/chain.pem"
173
+ end
174
+
175
+ def fullchain_key(cn, ver)
176
+ "#{certificate_base_key(cn, ver)}/fullchain.pem"
177
+ end
178
+
179
+ def get_media(media_link)
180
+ open(media_link, {'Authorization' => "Bearer #{@api.authorization.access_token}"}).read
181
+ end
182
+
183
+ def load_json_key(filepath)
184
+ obj = JSON.parse(File.read(filepath))
185
+ {
186
+ email_address: obj["client_email"],
187
+ private_key: OpenSSL::PKey.read(obj["private_key"]),
188
+ }
189
+ end
190
+ end
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acmesmith-google-cloud-storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - YAMADA Tsuyoshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: acmesmith
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-api-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: acmesmith plugin implementing google_cloud_storage storage
84
+ email:
85
+ - tyamada@minimum2scp.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - acmesmith-google-cloud-storage.gemspec
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/acmesmith-google-cloud-storage/version.rb
101
+ - lib/acmesmith/storages/google_cloud_storage.rb
102
+ homepage: https://github.com/minimum2scp/acmesmith-google-cloud-storage
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: acmesmith plugin implementing google_cloud_storage storage
126
+ test_files: []