Class: Acmesmith::Storages::GoogleCloudStorage

Inherits:
Base
  • Object
show all
Defined in:
lib/acmesmith/storages/google_cloud_storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, prefix: nil, compute_engine_service_account: nil, private_key_json_file: nil) ⇒ GoogleCloudStorage

Returns a new instance of GoogleCloudStorage.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 14

def initialize(bucket:, prefix:nil, compute_engine_service_account:nil, private_key_json_file:nil)
  @bucket = bucket
  @prefix = prefix
  if @prefix && !@prefix.end_with?('/')
    @prefix += '/'
  end
   = 
  @private_key_json_file = private_key_json_file

  @scope = 'https://www.googleapis.com/auth/devstorage.read_write'
  @api = Google::Apis::StorageV1::StorageService.new
  if 
    @api.authorization = Google::Auth.get_application_default(@scope)
  elsif @private_key_json_file
    credential = load_json_key(@private_key_json_file)
    @api.authorization = Signet::OAuth2::Client.new(
      token_credential_uri: "https://accounts.google.com/o/oauth2/token",
      audience: "https://accounts.google.com/o/oauth2/token",
      scope: @scope,
      issuer: credential[:email_address],
      signing_key: credential[:private_key])
  else
    raise "You need to specify authentication options (compute_engine_service_account or private_key_json_file)"
  end
  @api.authorization.fetch_access_token!
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



12
13
14
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 12

def bucket
  @bucket
end

#compute_engine_service_accountObject (readonly)

Returns the value of attribute compute_engine_service_account.



12
13
14
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 12

def 
  
end

#prefixObject (readonly)

Returns the value of attribute prefix.



12
13
14
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 12

def prefix
  @prefix
end

#private_key_json_fileObject (readonly)

Returns the value of attribute private_key_json_file.



12
13
14
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 12

def private_key_json_file
  @private_key_json_file
end

Instance Method Details

#account_key_exist?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 52

def 
  begin
    
  rescue NotExist
    return false
  else
    return true
  end
end

#get_account_keyObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 41

def 
  @api.get_object(bucket, )
  AccountKey.new @api.get_object(bucket, , download_dest: StringIO.new).string
rescue Google::Apis::ClientError => e
  if e.status_code == 404
    raise NotExist.new("Account key doesn't exist")
  else
    raise e
  end
end

#get_certificate(common_name, version: 'current') ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 96

def get_certificate(common_name, version: 'current')
  version = certificate_current(common_name) if version == 'current'

  get = ->(key) do
    @api.get_object(bucket, key, download_dest: StringIO.new).string
  end

  certificate = get.call(certificate_key(common_name, version))
  chain       = get.call(chain_key(common_name, version))
  private_key = get.call(private_key_key(common_name, version))
  Certificate.new(certificate, chain, private_key)
rescue Google::Apis::ClientError => e
  if e.status_code == 404
    raise NotExist.new("Certificate for #{common_name.inspect} of #{version} version doesn't exist")
  else
    raise e
  end
end

#get_current_certificate_version(common_name) ⇒ Object



147
148
149
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 147

def get_current_certificate_version(common_name)
  certificate_current(common_name)
end

#list_certificate_versions(common_name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 131

def list_certificate_versions(common_name)
  cert_ver_prefix = "#{prefix}certs/#{common_name}/"
  cert_ver_prefix_regexp = /\A#{Regexp.escape(cert_ver_prefix)}/
  list = []
  page_token = nil
  loop do
    objects = @api.list_objects(bucket, prefix: cert_ver_prefix, delimiter: '/', page_token: page_token)
    if objects.prefixes
      list.concat objects.prefixes.map{|_| _.sub(cert_ver_prefix_regexp, '').sub(/\/.+\z/, '').sub(/\/\z/, '') }
    end
    break if objects.next_page_token.nil? || objects.next_page_token == page_token
    page_token = objects.next_page_token
  end
  list.uniq.reject{ |_| _ == 'current' }
end

#list_certificatesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 115

def list_certificates
  certs_prefix = "#{prefix}certs/"
  certs_prefix_regexp = /\A#{Regexp.escape(certs_prefix)}/
  list = []
  page_token = nil
  loop do
    objects = @api.list_objects(bucket, prefix: certs_prefix, delimiter: '/', page_token: page_token)
    if objects.prefixes
      list.concat objects.prefixes.map{|_| _.sub(certs_prefix_regexp, '').sub(/\/.+\z/,'').sub(/\/\z/, '')}
    end
    break if objects.next_page_token.nil? || objects.next_page_token == page_token
    page_token = objects.next_page_token
  end
  list.uniq
end

#put_account_key(key, passphrase = nil) ⇒ Object

Raises:

  • (AlreadyExist)


62
63
64
65
66
67
68
69
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 62

def (key, passphrase = nil)
  raise AlreadyExist if 
  obj = Google::Apis::StorageV1::Object.new(
    name: ,
    content_type: 'application/x-pem-file'
  )
  @api.insert_object(bucket, obj, upload_source: StringIO.new(key.export(passphrase)))
end

#put_certificate(cert, passphrase = nil, update_current: true) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/acmesmith/storages/google_cloud_storage.rb', line 71

def put_certificate(cert, passphrase = nil, update_current: true)
  h = cert.export(passphrase)

  put = -> (key, body) do
    obj = Google::Apis::StorageV1::Object.new(
      name: key,
      content_type: 'application/x-pem-file',
    )
    @api.insert_object(bucket, obj, upload_source: StringIO.new(body))
  end

  put.call certificate_key(cert.common_name, cert.version), "#{h[:certificate].rstrip}\n"
  put.call chain_key(cert.common_name, cert.version), "#{h[:chain].rstrip}\n"
  put.call fullchain_key(cert.common_name, cert.version), "#{h[:fullchain].rstrip}\n"
  put.call private_key_key(cert.common_name, cert.version), "#{h[:private_key].rstrip}\n"

  if update_current
    @api.insert_object(
      bucket,
      Google::Apis::StorageV1::Object.new(name: certificate_current_key(cert.common_name), content_type: 'text/plain'),
      upload_source: StringIO.new(cert.version),
    )
  end
end