summaryrefslogtreecommitdiff
path: root/lib/chef/mixin/resource_credential.rb
blob: f2c2d6d7fa87afbb0825282888e1a37102108afc (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
65
66
67
68
69
70
71
#
# Author:: Adam Edwards (<adamed@chef.io>)
# Copyright:: Copyright (c) 2015 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.
#

class Chef
  module Mixin
    module ResourceCredential

      def validate_credential(specified_user, specified_domain, password)
        user = specified_user
        domain = specified_domain

        if Chef::Platform.windows?
          if ! user.nil?
            domain, user = canonicalize_credential(specified_domain, specified_user)
          end

          if ! user.nil? && password.nil?
            raise ArgumentError, "No `password` property was specified when the `user` property was specified"
          end
        elsif ! domain.nil? || ! password.nil?
          raise Exceptions::UnsupportedPlatform, "The `domain` and `password` properties are only supported on the Windows platform"
        end

        if ( ! password.nil? || ! domain.nil? ) && user.nil?
          raise ArgumentError, "The `password` or `domain` property was specified without specification of the user property"
        end
      end

      def canonicalize_credential(specified_domain, specified_user)
        domain = specified_domain
        user = specified_user

        if ! specified_user.nil? && specified_domain.nil?
          domain_and_user = user.split('\\')

          if domain_and_user.length == 1
            domain_and_user = user.split('@')
          end

          if domain_and_user.length == 2
            domain = domain_and_user[0]
            user = domain_and_user[1]
          elsif domain_and_user.length != 1
            raise ArgumentError, "The specified user name `#{user}` is not a syntactically valid user name"
          end
        end

        [domain, user]
      end

      private(:validate_credential)
      private(:canonicalize_credential)

    end
  end
end