terraspace_plugin_azurerm 0.2.3 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/lib/templates/hcl/project/config/terraform/backend.tf +1 -1
- data/lib/terraspace_plugin_azurerm/interfaces/backend/storage_container.rb +0 -1
- data/lib/terraspace_plugin_azurerm/interfaces/config.rb +2 -2
- data/lib/terraspace_plugin_azurerm/interfaces/helper/secret/fetcher.rb +148 -0
- data/lib/terraspace_plugin_azurerm/interfaces/helper/secret.rb +26 -0
- data/lib/terraspace_plugin_azurerm/interfaces/helper.rb +10 -0
- data/lib/terraspace_plugin_azurerm/logging.rb +7 -0
- data/lib/terraspace_plugin_azurerm/version.rb +1 -1
- data/lib/terraspace_plugin_azurerm.rb +10 -0
- data/terraspace_plugin_azurerm.gemspec +1 -1
- metadata +11 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33dd9bb20fc1cc57ccd1e31f46e52621838ad043b118e67061b0c7b0bf96bf9c
|
4
|
+
data.tar.gz: c3721a61c30ad15870c1858ada72199380c9628572f80a3a0576750266a65b90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 442becc7f22e2b5b3bf1dc6c621a1dd52af1d4696934c3e6fd9dfc83e4f5d723fb5b5aceb29d24ef47b5b7be939a2676d8d205f77422618a31c8f381ce5d468a
|
7
|
+
data.tar.gz: 3e10754d0616e4877c34435cf30da1452eb58cafa938d82d3326ded07670b01cf202d11410ea114a3bb21dc177acadaf9dc4a05b3a524108bb5865ad3fe16d62
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,19 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
|
5
5
|
|
6
|
+
## [0.3.3] - 2022-01-04
|
7
|
+
- [#10](https://github.com/boltops-tools/terraspace_plugin_azurerm/pull/10) azure_secret support expansion automatically
|
8
|
+
|
9
|
+
## [0.3.2] - 2021-11-29
|
10
|
+
- [#9](https://github.com/boltops-tools/terraspace_plugin_azurerm/pull/9) change starter resource_group_name to have env
|
11
|
+
|
12
|
+
## [0.3.1] - 2021-04-15
|
13
|
+
- update azure_info dependency
|
14
|
+
|
15
|
+
## [0.3.0] - 2020-11-15
|
16
|
+
- [#5](https://github.com/boltops-tools/terraspace_plugin_azurerm/pull/5) helper and secrets support
|
17
|
+
- azure_secret helper
|
18
|
+
|
6
19
|
## [0.2.3]
|
7
20
|
- #4 validate env vars and use older storage client
|
8
21
|
|
@@ -2,7 +2,7 @@
|
|
2
2
|
# This is useful because azure storage account names are not allowed special characters and are limited to 24 chars.
|
3
3
|
terraform {
|
4
4
|
backend "azurerm" {
|
5
|
-
resource_group_name = "<%= expansion('
|
5
|
+
resource_group_name = "<%= expansion(':ENV-:LOCATION') %>"
|
6
6
|
storage_account_name = "<%= expansion('ts:SUBSCRIPTION_HASH:LOCATION:ENV') %>"
|
7
7
|
container_name = "terraform-state"
|
8
8
|
key = "<%= expansion(':LOCATION/:ENV/:BUILD_DIR/terraform.tfstate') %>"
|
@@ -15,12 +15,12 @@ module TerraspacePluginAzurerm::Interfaces
|
|
15
15
|
c = ActiveSupport::OrderedOptions.new
|
16
16
|
c.auto_create = true
|
17
17
|
c.location = nil # AzureInfo.location not assigned here so it can be lazily inferred
|
18
|
-
|
18
|
+
c.secrets = ActiveSupport::OrderedOptions.new
|
19
|
+
c.secrets.vault = nil
|
19
20
|
c.storage_account = ActiveSupport::OrderedOptions.new
|
20
21
|
c.storage_account.sku = ActiveSupport::OrderedOptions.new
|
21
22
|
c.storage_account.sku.name = "Standard_LRS"
|
22
23
|
c.storage_account.sku.tier = "Standard"
|
23
|
-
|
24
24
|
c
|
25
25
|
end
|
26
26
|
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require "net/http"
|
2
|
+
|
3
|
+
class TerraspacePluginAzurerm::Interfaces::Helper::Secret
|
4
|
+
class Fetcher
|
5
|
+
class Error < StandardError; end
|
6
|
+
class VaultNotFoundError < Error; end
|
7
|
+
class VaultNotConfiguredError < Error; end
|
8
|
+
|
9
|
+
include TerraspacePluginAzurerm::Logging
|
10
|
+
include TerraspacePluginAzurerm::Clients::Options
|
11
|
+
extend Memoist
|
12
|
+
|
13
|
+
def initialize(mod, options={})
|
14
|
+
@mod, @options = mod, options
|
15
|
+
o = base_client_options
|
16
|
+
@client_id = o[:client_id]
|
17
|
+
@client_secret = o[:client_secret]
|
18
|
+
@tenant_id = o[:tenant_id]
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetch(name, opts={})
|
22
|
+
opts[:vault] ||= TerraspacePluginAzurerm.config.secrets.vault
|
23
|
+
get_secret(name, opts)
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_secret(name, options={})
|
27
|
+
vault = options[:vault]
|
28
|
+
version = options[:version]
|
29
|
+
unless token
|
30
|
+
return "ERROR: Unable to authorize and get the temporary token. Double check your ARM_ env variables."
|
31
|
+
end
|
32
|
+
|
33
|
+
version = "/#{version}" if version
|
34
|
+
check_vault_configured!(vault)
|
35
|
+
vault_subdomain = vault.downcase
|
36
|
+
# Using Azure REST API since the old gem doesnt support secrets https://github.com/Azure/azure-sdk-for-ruby
|
37
|
+
# https://docs.microsoft.com/en-us/rest/api/keyvault/getsecret/getsecret
|
38
|
+
name = expansion(name) if expand?
|
39
|
+
url = "https://#{vault_subdomain}.vault.azure.net/secrets/#{name}#{version}?api-version=7.1"
|
40
|
+
logger.debug "Azure vault url #{url}"
|
41
|
+
uri = URI(url)
|
42
|
+
req = Net::HTTP::Get.new(uri)
|
43
|
+
req["Authorization"] = token
|
44
|
+
req["Content-Type"] = "application/json"
|
45
|
+
|
46
|
+
resp = nil
|
47
|
+
begin
|
48
|
+
resp = send_request(uri, req)
|
49
|
+
rescue VaultNotFoundError
|
50
|
+
message = "WARN: Vault not found #{vault}"
|
51
|
+
logger.info message.color(:yellow)
|
52
|
+
return message
|
53
|
+
end
|
54
|
+
|
55
|
+
case resp.code.to_s
|
56
|
+
when /^2/
|
57
|
+
data = JSON.load(resp.body)
|
58
|
+
data['value']
|
59
|
+
else
|
60
|
+
message = standard_error_message(resp)
|
61
|
+
logger.info "WARN: #{message}".color(:yellow)
|
62
|
+
message
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def check_vault_configured!(vault)
|
67
|
+
return if vault
|
68
|
+
logger.error "ERROR: Vault has not been configured or vault option not passed in the azure_secret helper method.".color(:red)
|
69
|
+
logger.error <<~EOL
|
70
|
+
Please configure the Azure KeyVault you want to use. Example:
|
71
|
+
|
72
|
+
config/plugins/azurerm.rb
|
73
|
+
|
74
|
+
TerraspacePluginAzurerm.configure do |config|
|
75
|
+
config.secrets.vault = "REPLACE_WITH_YOUR_VAULT_NAME"
|
76
|
+
end
|
77
|
+
|
78
|
+
Docs: https://terraspace.cloud/docs/helpers/azure/secrets/
|
79
|
+
EOL
|
80
|
+
raise VaultNotConfiguredError.new
|
81
|
+
end
|
82
|
+
|
83
|
+
def send_request(uri, req)
|
84
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
85
|
+
http.open_timeout = http.read_timeout = 30
|
86
|
+
http.use_ssl = true if uri.scheme == 'https'
|
87
|
+
|
88
|
+
begin
|
89
|
+
http.request(req) # response
|
90
|
+
rescue SocketError => e
|
91
|
+
# SocketError: Failed to open TCP connection to MISSING-VAULT.vault.azure.net:443 (getaddrinfo: Name or service not known)
|
92
|
+
if e.message.include?("vault.azure.net")
|
93
|
+
raise VaultNotFoundError.new(e)
|
94
|
+
else
|
95
|
+
raise
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Secret error handling: 1. network 2. json parse 3. missing secret
|
101
|
+
#
|
102
|
+
# Azure API responses with decent error message when
|
103
|
+
# 403 Forbidden - KeyVault Access Policy needs to be set up
|
104
|
+
# 404 Not Found - Secret name is incorrect
|
105
|
+
#
|
106
|
+
def standard_error_message(resp)
|
107
|
+
data = JSON.load(resp.body)
|
108
|
+
data['error']['message']
|
109
|
+
rescue JSON::ParserError
|
110
|
+
resp.body
|
111
|
+
end
|
112
|
+
|
113
|
+
@@token = nil
|
114
|
+
def token
|
115
|
+
return @@token unless @@token.nil?
|
116
|
+
url = "https://login.microsoftonline.com/#{@tenant_id}/oauth2/token"
|
117
|
+
uri = URI(url)
|
118
|
+
req = Net::HTTP::Get.new(uri)
|
119
|
+
req.set_form_data(
|
120
|
+
grant_type: "client_credentials",
|
121
|
+
client_id: @client_id,
|
122
|
+
client_secret: @client_secret,
|
123
|
+
resource: "https://vault.azure.net",
|
124
|
+
)
|
125
|
+
resp = send_request(uri, req)
|
126
|
+
data = JSON.load(resp.body)
|
127
|
+
if resp.code =~ /^2/
|
128
|
+
@@token = "Bearer #{data['access_token']}" if data
|
129
|
+
else
|
130
|
+
logger.info "WARN: #{data['error_description']}".color(:yellow)
|
131
|
+
# return false otherwise error message is used as the bearer toke and get this error:
|
132
|
+
# ArgumentError: header field value cannot include CR/LF
|
133
|
+
@@token = false
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
delegate :expansion, to: :expander
|
139
|
+
def expander
|
140
|
+
TerraspacePluginAzurerm::Interfaces::Expander.new(@mod)
|
141
|
+
end
|
142
|
+
memoize :expander
|
143
|
+
|
144
|
+
def expand?
|
145
|
+
!(@options[:expansion] == false || @options[:expand] == false)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "base64"
|
2
|
+
|
3
|
+
module TerraspacePluginAzurerm::Interfaces::Helper
|
4
|
+
class Secret
|
5
|
+
extend Memoist
|
6
|
+
include TerraspacePluginAzurerm::Logging
|
7
|
+
include TerraspacePluginAzurerm::Clients::Options
|
8
|
+
|
9
|
+
def initialize(mod, options={})
|
10
|
+
@mod, @options = mod, options
|
11
|
+
@base64 = options[:base64]
|
12
|
+
end
|
13
|
+
|
14
|
+
# opts: version, vault
|
15
|
+
def fetch(name, opts={})
|
16
|
+
value = fetcher.fetch(name, opts)
|
17
|
+
value = Base64.strict_encode64(value).strip if @base64
|
18
|
+
value
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetcher
|
22
|
+
Fetcher.new(@mod, @options)
|
23
|
+
end
|
24
|
+
memoize :fetcher
|
25
|
+
end
|
26
|
+
end
|
@@ -23,12 +23,22 @@ module TerraspacePluginAzurerm
|
|
23
23
|
Interfaces::Config.instance.config
|
24
24
|
end
|
25
25
|
|
26
|
+
@@logger = nil
|
27
|
+
def logger
|
28
|
+
@@logger ||= Terraspace.logger
|
29
|
+
end
|
30
|
+
|
31
|
+
def logger=(v)
|
32
|
+
@@logger = v
|
33
|
+
end
|
34
|
+
|
26
35
|
extend self
|
27
36
|
end
|
28
37
|
|
29
38
|
Terraspace::Plugin.register("azurerm",
|
30
39
|
backend: "azurerm",
|
31
40
|
config_class: TerraspacePluginAzurerm::Interfaces::Config,
|
41
|
+
helper_class: TerraspacePluginAzurerm::Interfaces::Helper,
|
32
42
|
layer_class: TerraspacePluginAzurerm::Interfaces::Layer,
|
33
43
|
root: File.dirname(__dir__),
|
34
44
|
)
|
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.require_paths = ["lib"]
|
24
24
|
|
25
25
|
spec.add_dependency "azure-storage-blob"
|
26
|
-
spec.add_dependency "azure_info"
|
26
|
+
spec.add_dependency "azure_info", "~> 0.1.2"
|
27
27
|
spec.add_dependency "azure_mgmt_resources"
|
28
28
|
spec.add_dependency "azure_mgmt_storage"
|
29
29
|
spec.add_dependency "memoist"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terraspace_plugin_azurerm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: azure-storage-blob
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: azure_info
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 0.1.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 0.1.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: azure_mgmt_resources
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -151,8 +151,12 @@ files:
|
|
151
151
|
- lib/terraspace_plugin_azurerm/interfaces/backend/storage_container.rb
|
152
152
|
- lib/terraspace_plugin_azurerm/interfaces/config.rb
|
153
153
|
- lib/terraspace_plugin_azurerm/interfaces/expander.rb
|
154
|
+
- lib/terraspace_plugin_azurerm/interfaces/helper.rb
|
155
|
+
- lib/terraspace_plugin_azurerm/interfaces/helper/secret.rb
|
156
|
+
- lib/terraspace_plugin_azurerm/interfaces/helper/secret/fetcher.rb
|
154
157
|
- lib/terraspace_plugin_azurerm/interfaces/layer.rb
|
155
158
|
- lib/terraspace_plugin_azurerm/interfaces/summary.rb
|
159
|
+
- lib/terraspace_plugin_azurerm/logging.rb
|
156
160
|
- lib/terraspace_plugin_azurerm/version.rb
|
157
161
|
- terraspace_plugin_azurerm.gemspec
|
158
162
|
homepage: https://github.com/boltops-tools/terraspace_plugin_azurerm
|
@@ -175,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
179
|
- !ruby/object:Gem::Version
|
176
180
|
version: '0'
|
177
181
|
requirements: []
|
178
|
-
rubygems_version: 3.
|
182
|
+
rubygems_version: 3.2.32
|
179
183
|
signing_key:
|
180
184
|
specification_version: 4
|
181
185
|
summary: Terraspace Azurerm Cloud Plugin
|