oso-cloud 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f79239f03cdcaddbb6a8c9800b4c83a5d28b4acccd3cae2875a9585306c1531e
4
+ data.tar.gz: 59764d372207e4beda482bddf6f4489573a640d61ba9d064d2644931ffe6dde0
5
+ SHA512:
6
+ metadata.gz: f68bf249055cf164b296a945a3149e0b4328f6da4843105b9455c9e864dd87d7b25374cb8cc2d3c76f971368b9944bd72784e15f36564ba5f9ed89ca538f955e
7
+ data.tar.gz: be9850f671f35bc24f64061b9975e92aba2c66d5c6ce9c4b33bbf2027c4e4cba7b7e62bef2f82d1057b1ed15763022d51d32d8200397629050145e271520bc45
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ .bundle
3
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in oso-cloud.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Oso::Client
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/oso/client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'oso-cloud'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install oso-cloud
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "oso/client"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/oso/client.rb ADDED
@@ -0,0 +1,142 @@
1
+ require 'json'
2
+ require 'logger'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ require 'oso/version'
7
+
8
+ module Oso
9
+ class Client
10
+ def initialize(url: 'https://cloud.osohq.com', api_key: nil, logger: nil)
11
+ @url = url
12
+ @api_key = api_key
13
+ @logger = logger || Logger.new(STDOUT)
14
+ # TODO: why does this need to be configurable?
15
+ # @to_type_and_id = method(default_to_type_and_id)
16
+ end
17
+
18
+ def authorize(actor, action, resource)
19
+ actor_type, actor_id = default_to_type_and_id actor
20
+ resource_type, resource_id = default_to_type_and_id resource
21
+ result = post('authorize', {
22
+ actor_type: actor_type, actor_id: actor_id,
23
+ action: action,
24
+ resource_type: resource_type, resource_id: resource_id
25
+ })
26
+ allowed = result['allowed']
27
+ @logger.debug { "AUTHORIZING (#{actor}, #{action}, #{resource}) => ALLOWED? = #{allowed ? 'true' : 'false'} " }
28
+
29
+ allowed
30
+ end
31
+
32
+ def list(actor, action, resource_type)
33
+ actor_type, actor_id = default_to_type_and_id actor
34
+ result = post('list',
35
+ {
36
+ actor_type: actor_type, actor_id: actor_id,
37
+ action: action,
38
+ resource_type: resource_type
39
+ })
40
+ results = result['results']
41
+ @logger.debug { "AUTHORIZING (#{actor}, #{action}, #{resource_type}). RESULTS: #{results}" }
42
+
43
+ results
44
+ end
45
+
46
+ def add_role(actor, role_name, resource)
47
+ add_role_or_relation('role', resource, role_name, actor)
48
+ end
49
+
50
+ def delete_role(actor, role_name, resource)
51
+ delete_role_or_relation('role', resource, role_name, actor)
52
+ end
53
+
54
+ def add_relation(subject, name, object)
55
+ add_role_or_relation('relation', subject, name, object)
56
+ end
57
+
58
+ def delete_relation(subject, name, object)
59
+ delete_role_or_relation('relation', subject, name, object)
60
+ end
61
+
62
+ def get_roles(resource: nil, role: nil, actor: nil)
63
+ params = {}
64
+ unless actor.nil?
65
+ actor_type, actor_id = default_to_type_and_id actor
66
+ params[:actor_type] = actor_type
67
+ params[:actor_id] = actor_id
68
+ end
69
+ unless resource.nil?
70
+ resource_type, resource_id = default_to_type_and_id resource
71
+ params[:resource_type] = resource_type
72
+ params[:resource_id] = resource_id
73
+ end
74
+ params[:role] = role unless role.nil?
75
+
76
+ get('roles')
77
+ end
78
+
79
+ private
80
+
81
+ def auth()
82
+ {"Authorization" => "Basic %s" % @api_key}
83
+ end
84
+
85
+ def get(path)
86
+ result = Net::HTTP.get(URI("#{@url}/api/#{path}"), auth)
87
+ handle_result result
88
+ end
89
+
90
+ def post(path, params)
91
+ result = Net::HTTP.post(URI("#{@url}/api/#{path}"), params.to_json, auth)
92
+ handle_result result
93
+ end
94
+
95
+ def delete(path, params)
96
+ result = Net::HTTP.delete(URI("#{@url}/api/#{path}"), params.to_json, auth)
97
+ handle_result result
98
+ end
99
+
100
+ # TODO: why does this need to be configurable?
101
+ def default_to_type_and_id(obj)
102
+ if obj.nil?
103
+ %w[null null]
104
+ else
105
+ [obj.class.to_s, obj.id.to_s]
106
+ end
107
+ end
108
+
109
+ def handle_result(result)
110
+ unless result.is_a?(Net::HTTPSuccess)
111
+ raise "Got an unexpected error from Oso Service: #{result.code}\n#{result.body}"
112
+ end
113
+
114
+ # TODO: Always JSON?
115
+ JSON.parse(result.body)
116
+ end
117
+
118
+ def to_params(role_or_relation, from, name, to)
119
+ from_type, from_id = default_to_type_and_id from
120
+ to_type, to_id = default_to_type_and_id to
121
+
122
+ from_name = role_or_relation == 'role' ? 'resource' : 'from'
123
+ to_name = role_or_relation == 'role' ? 'actor' : 'to'
124
+
125
+ {
126
+ "#{from_name}_id" => from_id,
127
+ "#{from_name}_type" => from_type,
128
+ role_or_relation.to_s => name,
129
+ "#{to_name}_id" => to_id,
130
+ "#{to_name}_type" => to_type
131
+ }
132
+ end
133
+
134
+ def add_role_or_relation(role_or_relation, from, name, to)
135
+ post("#{role_or_relation}s", to_params(role_or_relation, from, name, to))
136
+ end
137
+
138
+ def delete_role_or_relation(role_or_relation, from, name, to)
139
+ delete("#{role_or_relation}s", to_params(role_or_relation, from, name, to))
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ module Oso
2
+ VERSION = '0.2.0'.freeze
3
+ end
data/oso-cloud.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ require_relative 'lib/oso/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'oso-cloud'
5
+ spec.version = Oso::VERSION
6
+ spec.authors = ['Oso Security, Inc.']
7
+ spec.email = ['support@osohq.com']
8
+ spec.summary = 'Oso authorization library.'
9
+ spec.homepage = 'https://www.osohq.com/'
10
+
11
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
12
+
13
+ # Specify which files should be added to the gem when it is released.
14
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
15
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oso-cloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Oso Security, Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - support@osohq.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - README.md
23
+ - Rakefile
24
+ - bin/console
25
+ - bin/setup
26
+ - lib/oso/client.rb
27
+ - lib/oso/version.rb
28
+ - oso-cloud.gemspec
29
+ homepage: https://www.osohq.com/
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.3.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.1.2
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Oso authorization library.
51
+ test_files: []