conjur-asset-dsl2 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.dockerignore +2 -0
  3. data/.gitignore +14 -0
  4. data/.project +18 -0
  5. data/.rspec +1 -0
  6. data/.travis.yml +4 -0
  7. data/CHANGELOG +1 -0
  8. data/Dockerfile.dev +19 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +248 -0
  12. data/Rakefile +18 -0
  13. data/backup.tar +0 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +7 -0
  16. data/conjur-asset-dsl2.gemspec +32 -0
  17. data/jenkins.sh +36 -0
  18. data/lib/conjur/command/dsl2.rb +175 -0
  19. data/lib/conjur/dsl2/executor/base.rb +50 -0
  20. data/lib/conjur/dsl2/executor/create.rb +117 -0
  21. data/lib/conjur/dsl2/executor/deny.rb +13 -0
  22. data/lib/conjur/dsl2/executor/give.rb +12 -0
  23. data/lib/conjur/dsl2/executor/grant.rb +13 -0
  24. data/lib/conjur/dsl2/executor/permit.rb +16 -0
  25. data/lib/conjur/dsl2/executor/retire.rb +7 -0
  26. data/lib/conjur/dsl2/executor/revoke.rb +11 -0
  27. data/lib/conjur/dsl2/executor/update.rb +31 -0
  28. data/lib/conjur/dsl2/executor.rb +99 -0
  29. data/lib/conjur/dsl2/invalid.rb +12 -0
  30. data/lib/conjur/dsl2/plan.rb +49 -0
  31. data/lib/conjur/dsl2/planner/base.rb +215 -0
  32. data/lib/conjur/dsl2/planner/grants.rb +85 -0
  33. data/lib/conjur/dsl2/planner/permissions.rb +80 -0
  34. data/lib/conjur/dsl2/planner/record.rb +102 -0
  35. data/lib/conjur/dsl2/planner.rb +38 -0
  36. data/lib/conjur/dsl2/ruby/loader.rb +263 -0
  37. data/lib/conjur/dsl2/types/base.rb +376 -0
  38. data/lib/conjur/dsl2/types/create.rb +15 -0
  39. data/lib/conjur/dsl2/types/deny.rb +17 -0
  40. data/lib/conjur/dsl2/types/give.rb +14 -0
  41. data/lib/conjur/dsl2/types/grant.rb +24 -0
  42. data/lib/conjur/dsl2/types/member.rb +14 -0
  43. data/lib/conjur/dsl2/types/permit.rb +22 -0
  44. data/lib/conjur/dsl2/types/policy.rb +129 -0
  45. data/lib/conjur/dsl2/types/records.rb +243 -0
  46. data/lib/conjur/dsl2/types/retire.rb +14 -0
  47. data/lib/conjur/dsl2/types/revoke.rb +14 -0
  48. data/lib/conjur/dsl2/types/update.rb +16 -0
  49. data/lib/conjur/dsl2/yaml/handler.rb +400 -0
  50. data/lib/conjur/dsl2/yaml/loader.rb +29 -0
  51. data/lib/conjur-asset-dsl2-version.rb +7 -0
  52. data/lib/conjur-asset-dsl2.rb +27 -0
  53. data/syntax.md +147 -0
  54. metadata +237 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f54a2250aa76f9ecd167caa91122a99ffeb388b
4
+ data.tar.gz: 2084a24c0bc0c6c64d4c7c91a552039e033edfd7
5
+ SHA512:
6
+ metadata.gz: de9819ba823e23fda6e4ee2053d369c3b981343eb933efac1b57e974ed9bf781626a3ecca761e6a30e2efe6bb0e466da2a461e336edfe9379428ac1533d741a8
7
+ data.tar.gz: 8792bf0c8f7208fdf60c4ab5854c8add51911f073e28e83f5a10555112c437a4ce7db336f20064432e9fdfecdb79ca9e8b3dc97596e0d769b893a50f70d90c76
data/.dockerignore ADDED
@@ -0,0 +1,2 @@
1
+ .git
2
+ tmp
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ plan*.yaml
2
+ plan*.yml
3
+ .DS_Store
4
+ *.txt
5
+ /.bundle/
6
+ /.yardoc
7
+ /Gemfile.lock
8
+ /_yardoc/
9
+ /coverage/
10
+ /doc/
11
+ /pkg/
12
+ /spec/reports/
13
+ /tmp/
14
+ /features/reports/
data/.project ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>conjur-asset-dsl2</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>com.aptana.ruby.core.rubynature</nature>
16
+ <nature>com.aptana.projects.webnature</nature>
17
+ </natures>
18
+ </projectDescription>
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -fdoc --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.5
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ * 0.3.0 Initial stable version
data/Dockerfile.dev ADDED
@@ -0,0 +1,19 @@
1
+ FROM registry.tld/conjur-appliance-cuke-master:4.6-stable
2
+
3
+ WORKDIR /src/conjur-asset-dsl2
4
+
5
+ RUN mkdir -p /src/conjur-asset-dsl2
6
+ RUN mkdir -p /src/conjur-asset-dsl2/lib
7
+ RUN mkdir -p /src/conjur-asset-dsl2/tmp
8
+
9
+ ADD Gemfile ./
10
+ ADD conjur-asset-dsl2.gemspec ./
11
+ ADD lib/conjur-asset-dsl2-version.rb ./lib/
12
+ RUN bundle
13
+ ADD . .
14
+
15
+ ENV CONJUR_AUTHN_LOGIN admin
16
+ ENV CONJUR_AUTHN_API_KEY secret
17
+ ENV CONJUR_ACCOUNT cucumber
18
+ ENV CONJUR_APPLIANCE_URL https://localhost/api
19
+ ENV CONJUR_CERT_FILE /opt/conjur/etc/ssl/ca.pem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in conjur-asset-dsl2.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kevin Gilpin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,248 @@
1
+ # Conjur `dsl2` plugin
2
+
3
+ This is a Conjur plugin for a next-generation DSL, used for both policies (self contained RBAC models) and entitlements (roles and permissions which span policies and global records).
4
+
5
+ The goals of the DSL are:
6
+
7
+ * Fully declarative
8
+ * Human and machine readable
9
+ * Simplified (relative to the older Ruby DSL)
10
+ * Safe to execute in any environment
11
+
12
+ The DSL basically supports the following high-level capabilities. Each one is idempotent (it can be run repeatedly without harmful-side effects):
13
+
14
+ * **Create / Update** records, such as Role, User, and Webservice
15
+ * **Grant** roles. This basic concept covers everything from group members to adding abstract roles. Grant list can be "exclusive", which revokes the role from anyone not in the list.
16
+ * **Permit** priviliges on resources. Each permission ("transaction" in RBAC parlance) consists of a role, a privilege, and a resource. Permission list can also be "exclusive".
17
+
18
+ Also possible:
19
+
20
+ * Update ownership of a record
21
+ * Revoke roles
22
+ * Deny privileges
23
+
24
+ # Functionality overview
25
+
26
+ ## `policy`
27
+
28
+ A `policy` definition creates a versioned policy role and resource. The policy role is the owner of all new records contained with in it.
29
+
30
+ In Ruby, when a DSL is loaded as a policy, the policy record is already created an in scope. Policy fields such as `id`, `records`, `permissions` etc can be populated directly.
31
+
32
+ ```ruby
33
+ policy "myapp/v1" do
34
+ body do
35
+ group "secrets-managers"
36
+
37
+ layer "webserver"
38
+ end
39
+ end
40
+ ```
41
+
42
+ In YAML:
43
+
44
+ ```yaml
45
+ - !policy
46
+ id: myapp/v1
47
+ ```
48
+
49
+ ## Create and Update Records
50
+
51
+ In Ruby, record create/update is enclosed in a `records` block. Each record is created by calling a function with the record `kind`, passing the record id as the argument. Attributes and annotations can be set in a block.
52
+
53
+
54
+ ```ruby
55
+ user "alice"
56
+
57
+ user "bob" do
58
+ uidnumber 1001
59
+ annotation "email", "bob@mycorp.com"
60
+ end
61
+ ```
62
+
63
+ Here's how to create two users in YAML:
64
+
65
+ ```yaml
66
+ - !user alice
67
+ - !user
68
+ id: bob
69
+ ```
70
+
71
+ The type of record that you want to create is indicated by the YAML tag. The id of the record can either be specified inline (like the first example), or as an explicit `id` field (like the second example).
72
+
73
+ ## Role members
74
+
75
+ `grant` is used to grant roles, which includes group membership.
76
+
77
+ An example in which `alice` and the `ops` group are the only members of the `developers` group.
78
+
79
+ ```ruby
80
+ grant do
81
+ role group("developers")
82
+ member user("alice")
83
+ member group("ops)", admin:true
84
+ exclusive true
85
+ end
86
+ ```
87
+
88
+ And in YAML:
89
+
90
+
91
+ ```yaml
92
+ - !grant
93
+ role: !group developers
94
+ members:
95
+ - !user alice
96
+ -
97
+ role: !group ops
98
+ admin: true
99
+ exclusive: true
100
+ ```
101
+
102
+ A member is composed of the `role` (or `roles`) being granted and the `member` (or `members`) which will get the role.
103
+
104
+ The `member` can be a plain role (again using the YAML tag to indicate the record type), if the role is granted without admin capability. To grant a role with admin, the role member is a structured entry composed of the `role` and the `admin` flag.
105
+
106
+ Note that when the `exclusive` feature is used, any existing role members that are **not** specified in the policy will be revoked. So in the example above, `!user alice` and `!group ops` will be the *only* members of `!group developers`.
107
+
108
+ ## Permissions
109
+
110
+ Like `grant` is used to grant roles, `permit` is used to give permissions on a resource.
111
+
112
+ ```ruby
113
+ permit %w(read execute) do
114
+ resource variable("db-password")
115
+ role group("developers")
116
+ role layer("app-server")
117
+ end
118
+
119
+ permit "update" do
120
+ resource variable("db-password")
121
+ role group("developers")
122
+ exclusive: true
123
+ end
124
+ ```
125
+
126
+ ```yaml
127
+ # developers group and the app-server layer are
128
+ # the only roles which can read and execute the secret.
129
+ - !permit
130
+ resource: !variable db-password
131
+ privilege: [ read, execute ]
132
+ roles:
133
+ - !group developers
134
+ - !layer app-server
135
+
136
+ # developers is the only role which can update the secret.
137
+ - !permit
138
+ resource: !variable db-password
139
+ privilege: update
140
+ role: !group developers
141
+ exclusive: true
142
+ ```
143
+
144
+ Use `deny` to remove a privilege without affecting the other privileges:
145
+
146
+ ```ruby
147
+ deny %w(read execute) do
148
+ resource variable("db-password")
149
+ role layer("app-server")
150
+ end
151
+ ```
152
+
153
+ In YAML:
154
+
155
+ ```yaml
156
+ - !deny
157
+ resource: !variable dev/db-password
158
+ privilege: [ read, execute ]
159
+ role: !layer dev/app-server
160
+ ```
161
+
162
+ # Ownership
163
+
164
+ Ownership of a record (or group of records) can be assigned using the `owner` field:
165
+
166
+ ```ruby
167
+ variable "db_password" do
168
+ owner group("developers")
169
+ end
170
+ ```
171
+
172
+ In YAML:
173
+
174
+ ```yaml
175
+ - !variable
176
+ id: db_password
177
+ owner: !group developers
178
+ ```
179
+
180
+ The owner tag will update both:
181
+
182
+ * **resource owner** the role will be given ownership of the `record` resource.
183
+ * **role owner** if the record has a corresponding role, the `owner` will be given the record role with `admin` option.
184
+
185
+ # Expanded discussion of design goals
186
+
187
+ This DSL format is designed to work better within automated policy management frameworks. Using these declaractive policy files, the entire authorization model of Conjur can be managed using policies.
188
+
189
+ Whenever Conjur needs to be changed, a new policy is created or an existing policy is modified. This policy is typically managed through standard source control techniques (e.g. Git pull requests), with the security team having authority to approve and merge.
190
+
191
+ In this way, management of a Conjur system can be treated as code and leverage corresponding best pratices such as branches, pull requests, post-receive hooks, repository permissions and access rights, etc.
192
+
193
+ In addition, because the DSL format (YAML) is machine-readable, it will be straightforward to develop visual tools for editing and managing policies. Automated generation of policy files is also simple.
194
+
195
+ # Benefits
196
+
197
+ These are the benefits of the policy DSL, as imagined internally by the Conjur team:
198
+
199
+ * Large permission changes are described in a coherent way (modification of many corresponding rules can be described in single policy)
200
+ * The history of permission changes is more clear and easier to track. For example, it’s easy to list and view all policies which included references to particular ID, and understand how and why specific permissions were applied/revoked. With the current CLI it’s possible to only figure out the operations done on particular object, but not the bigger context (probably involving many corresponding changes on other objects) in which they were applied.
201
+ * Policies can be formally validated before deployment
202
+ * Policies will implement `dry run` mode which shows the changes that will be applied to Conjur.
203
+ * Policies can be machine-generated:
204
+ * It's easy to provision many similar assets at once
205
+ * It's easy to generate and deploy policies from within configuration scripts
206
+ * It will be possible and easy to write custom ‘access management’ services, which would allow users to modify some permissions and create assets in Conjur, but will be able to enforce additional fine-grained restrictions, such as id naming conventions, etc.
207
+ * It will be possible and easy to write custom ‘policy builders’. After all, policy is just a data structure, which can be generated by any code.
208
+ * Deprovisioning of users is robust, and does not violate consistency of the database
209
+ * Export and import of permission models will be very straightforward, making it possible to implement Conjur “staging” setups.
210
+
211
+ # Examples
212
+
213
+ For many examples of sample policy files, see the [examples directory](https://github.com/conjurinc/conjur-asset-dsl2/tree/master/spec/lib/round-trip).
214
+
215
+ # Policy conflicts
216
+
217
+ Please note that it's pretty easy to write policies which say contradictory things. For example, Policy A might use `!members` to control the members of the developers group. Another Policy B might use `!grant` to add a specific user to the developers group. When Policy B runs, it will add the user to the group. When Policy A runs, it will revoke the user. If B is run again, the user will be re-added.
218
+
219
+ So usually good ensure that the members of a role and the privileges on a resource are managed by one approach or the other, but not both.
220
+
221
+ ## Installation
222
+
223
+ Add the plugin to Conjur:
224
+
225
+ ```sh-session
226
+ $ sudo -E conjur plugin install dsl2
227
+ ```
228
+
229
+ ## Development
230
+
231
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
232
+
233
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
234
+
235
+ ## Todo
236
+
237
+ * Planner : implement change of ownership for roles.
238
+ * Planner : verify that all records referenced by permissions and grants will exist (either pre-existing, or will be created by the policy).
239
+
240
+ ## Contributing
241
+
242
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/conjur-asset-dsl2.
243
+
244
+
245
+ ## License
246
+
247
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
248
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'ci/reporter/rake/rspec'
4
+ require 'cucumber'
5
+ require 'cucumber/rake/task'
6
+
7
+ RSpec::Core::RakeTask.new :spec
8
+ Cucumber::Rake::Task.new :features
9
+
10
+ task :jenkins => ['ci:setup:rspec', :spec] do
11
+ Cucumber::Rake::Task.new do |t|
12
+ t.cucumber_opts = "--tags ~@wip --format progress --format junit --out features/reports"
13
+ end.runner.run
14
+
15
+
16
+ end
17
+
18
+ task default: [:spec, :features]
data/backup.tar ADDED
Binary file
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "conjur-asset-dsl2"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ #require "irb"
14
+ #IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'conjur-asset-dsl2-version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "conjur-asset-dsl2"
8
+ spec.version = Conjur::Asset::DSL2::VERSION
9
+ spec.authors = ["Kevin Gilpin"]
10
+ spec.email = ["kgilpin@conjur.net"]
11
+
12
+ spec.summary = %q{A fully declarative DSL for Conjur with Ruby and YAML syntax.}
13
+ spec.homepage = "https://github.com/conjurinc/conjur-asset-dsl2"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "safe_yaml"
22
+
23
+ spec.add_development_dependency "conjur-cli"
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec-expectations"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "json_spec"
29
+ spec.add_development_dependency "cucumber"
30
+ spec.add_development_dependency "ci_reporter_rspec"
31
+ spec.add_development_dependency "aruba"
32
+ end
data/jenkins.sh ADDED
@@ -0,0 +1,36 @@
1
+ #!/bin/bash -e
2
+
3
+ mkdir -p tmp
4
+
5
+ function wait_for_conjur {
6
+ docker pull registry.tld/wait-for-conjur
7
+ docker run -i --rm --link $cid:conjur registry.tld/wait-for-conjur
8
+ }
9
+
10
+ PROJECT=conjur-asset-dsl2
11
+ BASE_IMAGE=registry.tld/conjur-appliance-cuke-master:4.6-stable
12
+ docker pull $BASE_IMAGE
13
+
14
+ cid_file=tmp/$PROJECT-dev.cid
15
+
16
+ docker build -t $PROJECT-dev -f Dockerfile.dev .
17
+
18
+ docker run \
19
+ -d \
20
+ --cidfile=$cid_file \
21
+ -v $PWD:/src/conjur-asset-dsl2 \
22
+ $PROJECT-dev
23
+
24
+ cid=$(cat $cid_file)
25
+
26
+ function finish {
27
+ rm -f $cid_file
28
+ docker rm -f $cid
29
+ }
30
+
31
+ wait_for_conjur
32
+
33
+
34
+ trap finish EXIT
35
+
36
+ docker exec $cid bash -c "bundle exec rake jenkins" || true
@@ -0,0 +1,175 @@
1
+ #
2
+ # Copyright (C) 2014 Conjur Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ # this software and associated documentation files (the "Software"), to deal in
6
+ # the Software without restriction, including without limitation the rights to
7
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
+ # the Software, and to permit persons to whom the Software is furnished to do so,
9
+ # subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ #
21
+ require 'conjur-asset-dsl2'
22
+
23
+ class Conjur::Command::DSL2 < Conjur::DSLCommand
24
+ def self.load filename, syntax
25
+ script = script_from_filename filename
26
+ loader(filename, syntax).load script, filename
27
+ end
28
+
29
+ def self.script_from_filename filename
30
+ if filename
31
+ if File.exists?(filename)
32
+ File.read(filename)
33
+ else
34
+ require 'open-uri'
35
+ uri = URI.parse(filename)
36
+ raise "Unable to read this kind of URL : #{filename}" unless uri.respond_to?(:read)
37
+ begin
38
+ uri.read
39
+ rescue OpenURI::HTTPError
40
+ raise "Unable to read URI #{filename} : #{$!.message}"
41
+ end
42
+ end
43
+ else
44
+ STDIN.read
45
+ end
46
+ end
47
+
48
+ def self.loader filename, syntax
49
+ if syntax.nil? && filename
50
+ filename =~ /\.([^.]+)$/
51
+ syntax = $1
52
+ end
53
+ raise "No syntax provided or detected" unless syntax
54
+ syntax = case syntax
55
+ when 'yaml', 'yml'
56
+ 'YAML'
57
+ when 'rb', 'ruby'
58
+ 'Ruby'
59
+ end
60
+ mod = Conjur::DSL2.const_get syntax
61
+ mod.const_get "Loader"
62
+ end
63
+
64
+ def self.execute api, records
65
+ actions = []
66
+ records.each do |record|
67
+ executor = Conjur::DSL2::Executor.class_for(record).new(record, actions, Conjur::Core::API.conjur_account)
68
+ executor.execute
69
+ end
70
+ Conjur::DSL2::HTTPExecutor.new(api).execute actions
71
+ end
72
+
73
+ desc "Load a DSL2 policy"
74
+ command :policy2 do |policy|
75
+
76
+ policy.desc "Load a policy from Conjur YAML DSL"
77
+ policy.long_desc <<-DESC
78
+ Using this command, Conjur data can be specified as Ruby or YAML statements and
79
+ loaded into the server.
80
+
81
+ Each statement performs one of the following functions:
82
+
83
+ * Find or create a record, for example a group
84
+
85
+ * Give a permission on a resource, e.g. permission to 'execute' a variable
86
+
87
+ * Grant a role, e.g. add a member to a group.
88
+
89
+ When finding or creating a record, the "namespace" option can be used to prepend
90
+ a common prefix to each record.
91
+
92
+ If the statements are enclosed by a "policy", the id of the policy is also prepended
93
+ to the id of each record, after the namespace.
94
+
95
+ This command can load the policy directly into Conjur, and it can also operate
96
+ in "dry run" mode. In dry run mode, an execution plan will be computed and printed,
97
+ but the actions will not be performed. The execution plan includes only the minimal
98
+ set of commands which are required to apply the policy to Conjur. In effect, it's
99
+ a "diff" between the policy and the current state of the Conjur database.
100
+
101
+ The execution plan can be printed in machine-readable YAML format, or in a more
102
+ human-friendly text format.
103
+
104
+ The YAML output of dry run mode can be used as input for the "conjur policy import"
105
+ command. Therefore, a policy can be loaded in three steps, if desired:
106
+
107
+ 1) Load the policy in dry run mode to print the execution plan.
108
+
109
+ 2) Review the execution plan, manually or programatically.
110
+
111
+ 3) Import the execution plan.
112
+ DESC
113
+ policy.arg_name "(policy-file | STDIN)"
114
+ policy.command :load do |c|
115
+
116
+ # Undefine options which are declared in the base (default) implementation.
117
+ # TODO: This code can be removed if and when dsl2 becomes the default.
118
+ %w(as-group as-role collection context c).each do |switch|
119
+ c.switches.delete switch.to_sym
120
+ c.flags.delete switch.to_sym
121
+ c.switches_declaration_order.delete_if{|s| s.name == switch.to_sym}
122
+ c.flags_declaration_order.delete_if{|s| s.name == switch.to_sym}
123
+ end
124
+
125
+ acting_as_option(c)
126
+
127
+ c.desc "Policy namespace (optional)"
128
+ c.flag [:namespace]
129
+
130
+ c.desc "Syntax (ruby or YAML, will be auto-detected from file extension)"
131
+ c.flag [:"syntax"]
132
+
133
+ c.desc "Print the actions that would be performed"
134
+ c.switch [:"dry-run"]
135
+
136
+ c.desc "Output format of --dry-run mode (text, yaml)"
137
+ c.default_value "yaml"
138
+ c.flag [:"format"]
139
+
140
+ c.action do |global_options,options,args|
141
+ Conjur.log = "stderr"
142
+
143
+ filename = args.pop
144
+ records = load filename, options[:syntax]
145
+ plan = Conjur::DSL2::Planner.plan(records, api, options.slice(:namespace, :ownerid))
146
+
147
+ if options[:"dry-run"]
148
+ case options[:"format"]
149
+ when 'text'
150
+ puts plan.actions.map(&:to_s)
151
+ else
152
+ puts plan.actions.to_yaml
153
+ end
154
+ else
155
+ execute api, plan.actions
156
+ end
157
+ end
158
+ end
159
+
160
+ policy.desc "Import policy statements from a policy plan (aka --dry-run)"
161
+ policy.arg_name "(statements-file | STDIN)"
162
+ policy.command :import do |c|
163
+ acting_as_option(c)
164
+
165
+ c.action do |global_options,options,args|
166
+ Conjur.log = "stderr"
167
+
168
+ filename = args.pop
169
+ script = script_from_filename filename
170
+ actions = YAML.load(script, filename)
171
+ execute api, actions, options
172
+ end
173
+ end
174
+ end
175
+ end