summaryrefslogtreecommitdiff
path: root/lib/chef/dsl/secret.rb
blob: 9cc7d6b3da37f48a4ae7a33a7739b62f9b247b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# Author:: Marc Paradise (<marc@chef.io>)
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative "../secret_fetcher"

class Chef
  module DSL
    module Secret

      # Helper method which looks up a secret using the given service and configuration,
      # and returns the retrieved secret value.
      # This DSL providers a wrapper around [Chef::SecretFetcher]
      #
      # Use of the secret helper in the context of a resource block will automatically mark
      # that resource as 'sensitive', preventing resource data from being logged.  See [Chef::Resource#sensitive].
      #
      # @option name [Object] The identifier or name for this secret
      # @option service [Symbol] The service identifier for the service that will
      #                         perform the secret lookup
      # @option config [Hash] The configuration that the named service expects
      #
      # @return result [Object] The response object type is determined by the fetcher. See fetcher documentation
      # to know what to expect for a given service.
      #
      # @example
      #
      # This example uses the built-in :example secret manager service, which
      # accepts a hash of secrets.
      #
      #   value = secret(name: "test1", service: :example, config: { "test1" => "value1" })
      #   log "My secret is #{value}"
      #
      #   value = secret(name: "test1", service: :aws_secrets_manager, config: { region: "us-west-1" })
      #   log "My secret is #{value.secret_string}"
      #
      # @note
      #
      # This is pretty straightforward, but should also extend nicely to support
      # named config (as 'service') with override config. Some future potential
      # usage examples:
      #   value = secret(name: "test1") # If a default is configured
      #   value = secret(name: "test1", service: "my_aws_east")
      #   value = secret(name: "test1", service: "my_aws_west", config: { region: "override-region" })
      def secret(name: nil, service: nil, config: nil)
        sensitive(true) if is_a?(Chef::Resource)
        Chef::SecretFetcher.for_service(service, config).fetch(name)
      end
    end
  end
end