Class: UnimatrixCLI::Keymaker::Policy::WriteCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/unimatrix_cli/keymaker/policy/write_command.rb

Instance Method Summary collapse

Methods inherited from Command

available_commands, descendants, #initialize, #read_file, #validate, #validate_collection, #write

Methods included from UnimatrixParser

included

Constructor Details

This class inherits a constructor from UnimatrixCLI::Command

Instance Method Details

#executeObject



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/unimatrix_cli/keymaker/policy/write_command.rb', line 20

def execute
  if ( @options[ :file ] && @options[ :policy ].nil? ) ||
     ( @options[ :file ].nil? && @options[ :policy ] )
     
    if @options[ :file ]
      contents = read_file( @options[ :file ] )
      
      if contents.respond_to?( :each )
        total_policies   = 0
        policies_created = 0
        
        contents.each do | resource_server |
          resource_server_name = resource_server[ 'resource_server' ]
          
          if resource_server_name && resource_server[ 'resources' ].respond_to?( :each )
            resource_server[ 'resources' ].each do | resource |
              resource_name   = resource[ 'code_name' ]
              actions         = resource[ 'actions' ]
              
              if resource_name && actions && actions.is_a?( Array ) && actions.present?
                total_policies += 1
                
                resource_string = "realm/#{ @options[ :realm ] }::" + 
                  "#{ resource_server_name }::#{ resource_name }/*"
                
                write_response = write_policy(
                  resource_string,
                  actions
                )
                
                if !write_response[ 'error' ]
                  policies_created += 1
                else
                  write(
                    message: "\n**Error writing policy**\n" +
                    "Resource String: #{ resource_string }\n" +
                    "Actions: #{ actions }\n" +
                    "Error: #{ write_response.inspect }\n\n", error: true
                  )
                end
              else
                write( 
                  message: "Resources for #{ resource_server_name } empty " +
                  "or not correctly formatted", error: true 
                )
              end
            end
          else
            write( 
              message: "File missing resource server name or is not " +
              "correctly formatted", error: true 
            )
          end
        end
        
        write(
          message: "Successfully written #{ policies_created } out of " +
          "#{ total_policies } policies.\n\n" +
          "Check policies here: #{ Configuration.default_config[ 'keymaker_url' ] }" +
          "/policies?access_token=#{ Configuration.access_token }&" +
          "resource_owner_uuid=#{ @options[ :resource_owner_uuid ] }"
        )
      else
        write( message: "File empty or not correctly formatted", error: true )
      end
    else
      resource_string, actions = build_from_policy_string( @options[ :policy ] )
      
      if resource_string && actions
        write_response = write_policy( resource_string, actions )
        
        if !write_response[ 'error' ]
          write(
            message: "Successfully written policy\n" +
            "#{ write_response[ 'resource' ] }\n" +
            "#{ write_response[ 'actions' ] }"
          )
        else
          write(
            message: "\n**Error writing policy**\n" +
            "Resource String: #{ resource_string }\n" +
            "Actions: #{ actions }\n" +
            "Error: #{ write_response.inspect }\n\n", error: true
          )
        end
      else
        write(
          message: "Error: unable to format policy from given input: #{ @options[ :policy ] }",
          error: true
        )
      end
    end
  else
    write(
      message: "Error: either option --file OR --policy must be specified.",
      error: true
    )
  end
end