google-cloud-storage 1.18.1 → 1.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/AUTHENTICATION.md +17 -30
- data/CHANGELOG.md +312 -0
- data/CONTRIBUTING.md +4 -5
- data/LOGGING.md +1 -1
- data/OVERVIEW.md +37 -5
- data/TROUBLESHOOTING.md +2 -8
- data/lib/google/cloud/storage/bucket/acl.rb +40 -40
- data/lib/google/cloud/storage/bucket/cors.rb +4 -1
- data/lib/google/cloud/storage/bucket/lifecycle.rb +259 -44
- data/lib/google/cloud/storage/bucket/list.rb +3 -3
- data/lib/google/cloud/storage/bucket.rb +1096 -172
- data/lib/google/cloud/storage/convert.rb +4 -3
- data/lib/google/cloud/storage/credentials.rb +16 -14
- data/lib/google/cloud/storage/errors.rb +7 -2
- data/lib/google/cloud/storage/file/acl.rb +181 -20
- data/lib/google/cloud/storage/file/list.rb +10 -8
- data/lib/google/cloud/storage/file/signer_v2.rb +36 -18
- data/lib/google/cloud/storage/file/signer_v4.rb +249 -61
- data/lib/google/cloud/storage/file/verifier.rb +2 -2
- data/lib/google/cloud/storage/file.rb +450 -84
- data/lib/google/cloud/storage/hmac_key/list.rb +182 -0
- data/lib/google/cloud/storage/hmac_key.rb +316 -0
- data/lib/google/cloud/storage/policy/binding.rb +246 -0
- data/lib/google/cloud/storage/policy/bindings.rb +196 -0
- data/lib/google/cloud/storage/policy/condition.rb +138 -0
- data/lib/google/cloud/storage/policy.rb +277 -24
- data/lib/google/cloud/storage/post_object.rb +20 -2
- data/lib/google/cloud/storage/project.rb +249 -50
- data/lib/google/cloud/storage/service.rb +479 -288
- data/lib/google/cloud/storage/version.rb +1 -1
- data/lib/google/cloud/storage.rb +86 -16
- data/lib/google-cloud-storage.rb +54 -7
- metadata +74 -27
@@ -0,0 +1,246 @@
|
|
1
|
+
# Copyright 2019 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
require "google/cloud/storage/policy/condition"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Storage
|
21
|
+
class Policy
|
22
|
+
##
|
23
|
+
# # Binding
|
24
|
+
#
|
25
|
+
# Value object associating members and an optional condition with a role.
|
26
|
+
#
|
27
|
+
# @see https://cloud.google.com/iam/docs/overview Cloud IAM Overview
|
28
|
+
#
|
29
|
+
# @attr [String] role Role that is assigned to members. For example,
|
30
|
+
# `roles/viewer`, `roles/editor`, or `roles/owner`. Required.
|
31
|
+
# @attr [Array<String>] members Specifies the identities requesting
|
32
|
+
# access for a Cloud Platform resource. members can have the
|
33
|
+
# following values. Required.
|
34
|
+
#
|
35
|
+
# * `allUsers`: A special identifier that represents anyone who is on
|
36
|
+
# the internet; with or without a Google account.
|
37
|
+
# * `allAuthenticatedUsers`: A special identifier that represents
|
38
|
+
# anyone who is authenticated with a Google account or a service
|
39
|
+
# account.
|
40
|
+
# * `user:{emailid}`: An email address that represents a specific
|
41
|
+
# Google account. For example, `alice@example.com`.
|
42
|
+
# * `serviceAccount:{emailid}`: An email address that represents a
|
43
|
+
# service account. For example, `my-other-app@appspot.gserviceaccount.com`.
|
44
|
+
# * `group:{emailid}`: An email address that represents a Google group.
|
45
|
+
# For example, `admins@example.com`.
|
46
|
+
# * `domain:{domain}`: The G Suite domain (primary) that represents
|
47
|
+
# all the users of that domain. For example, `google.com` or
|
48
|
+
# `example.com`. Required.
|
49
|
+
#
|
50
|
+
# @attr [Google::Cloud::Storage::Policy::Condition, nil] condition The
|
51
|
+
# condition that is associated with this binding, or `nil` if there is
|
52
|
+
# no condition. NOTE: An unsatisfied condition will not allow user
|
53
|
+
# access via current binding. Different bindings, including their
|
54
|
+
# conditions, are examined independently.
|
55
|
+
#
|
56
|
+
# @example
|
57
|
+
# require "google/cloud/storage"
|
58
|
+
#
|
59
|
+
# storage = Google::Cloud::Storage.new
|
60
|
+
# bucket = storage.bucket "my-bucket"
|
61
|
+
#
|
62
|
+
# policy = bucket.policy requested_policy_version: 3
|
63
|
+
# policy.bindings.each do |binding|
|
64
|
+
# puts binding.role
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# @example Updating a Policy from version 1 to version 3:
|
68
|
+
# require "google/cloud/storage"
|
69
|
+
#
|
70
|
+
# storage = Google::Cloud::Storage.new
|
71
|
+
# bucket = storage.bucket "my-bucket"
|
72
|
+
#
|
73
|
+
# bucket.uniform_bucket_level_access = true
|
74
|
+
#
|
75
|
+
# bucket.policy requested_policy_version: 3 do |p|
|
76
|
+
# p.version # the value is 1
|
77
|
+
# p.version = 3 # Must be explicitly set to opt-in to support for conditions.
|
78
|
+
#
|
79
|
+
# expr = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"
|
80
|
+
# p.bindings.insert({
|
81
|
+
# role: "roles/storage.admin",
|
82
|
+
# members: ["user:owner@example.com"],
|
83
|
+
# condition: {
|
84
|
+
# title: "my-condition",
|
85
|
+
# description: "description of condition",
|
86
|
+
# expression: expr
|
87
|
+
# }
|
88
|
+
# })
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
class Binding
|
92
|
+
attr_reader :role
|
93
|
+
attr_reader :members
|
94
|
+
attr_reader :condition
|
95
|
+
|
96
|
+
##
|
97
|
+
# Creates a Binding object.
|
98
|
+
#
|
99
|
+
# @param [String] role Role that is assigned to members. For example,
|
100
|
+
# `roles/viewer`, `roles/editor`, or `roles/owner`. Required.
|
101
|
+
# @param [Array<String>] members Specifies the identities requesting
|
102
|
+
# access for a Cloud Platform resource. members can have the
|
103
|
+
# following values. Required.
|
104
|
+
#
|
105
|
+
# * `allUsers`: A special identifier that represents anyone who is on
|
106
|
+
# the internet; with or without a Google account.
|
107
|
+
# * `allAuthenticatedUsers`: A special identifier that represents
|
108
|
+
# anyone who is authenticated with a Google account or a service
|
109
|
+
# account.
|
110
|
+
# * `user:{emailid}`: An email address that represents a specific
|
111
|
+
# Google account. For example, `alice@example.com`.
|
112
|
+
# * `serviceAccount:{emailid}`: An email address that represents a
|
113
|
+
# service account. For example, `my-other-app@appspot.gserviceaccount.com`.
|
114
|
+
# * `group:{emailid}`: An email address that represents a Google group.
|
115
|
+
# For example, `admins@example.com`.
|
116
|
+
# * `domain:{domain}`: The G Suite domain (primary) that represents
|
117
|
+
# all the users of that domain. For example, `google.com` or
|
118
|
+
# `example.com`. Required.
|
119
|
+
#
|
120
|
+
# @param [Google::Cloud::Storage::Policy::Condition] condition The
|
121
|
+
# condition that is associated with this binding. NOTE: An unsatisfied
|
122
|
+
# condition will not allow user access via current binding. Different
|
123
|
+
# bindings, including their conditions, are examined independently.
|
124
|
+
# Optional.
|
125
|
+
#
|
126
|
+
def initialize role:, members:, condition: nil
|
127
|
+
@role = String role
|
128
|
+
|
129
|
+
@members = Array members
|
130
|
+
raise ArgumentError, "members is empty, must be provided" if @members.empty?
|
131
|
+
|
132
|
+
condition = Condition.new(**condition) if condition.is_a? Hash
|
133
|
+
if condition && !(condition.is_a? Condition)
|
134
|
+
raise ArgumentError, "expected Condition, not #{condition.inspect}"
|
135
|
+
end
|
136
|
+
@condition = condition
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# Sets the role for the binding.
|
141
|
+
#
|
142
|
+
# @param [String] new_role Role that is assigned to members. For example,
|
143
|
+
# `roles/viewer`, `roles/editor`, or `roles/owner`. Required.
|
144
|
+
#
|
145
|
+
def role= new_role
|
146
|
+
@role = String new_role
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# Sets the members for the binding.
|
151
|
+
#
|
152
|
+
# @param [Array<String>] new_members Specifies the identities requesting
|
153
|
+
# access for a Cloud Platform resource. members can have the
|
154
|
+
# following values. Required.
|
155
|
+
#
|
156
|
+
# * `allUsers`: A special identifier that represents anyone who is on
|
157
|
+
# the internet; with or without a Google account.
|
158
|
+
# * `allAuthenticatedUsers`: A special identifier that represents
|
159
|
+
# anyone who is authenticated with a Google account or a service
|
160
|
+
# account.
|
161
|
+
# * `user:{emailid}`: An email address that represents a specific
|
162
|
+
# Google account. For example, `alice@example.com`.
|
163
|
+
# * `serviceAccount:{emailid}`: An email address that represents a
|
164
|
+
# service account. For example, `my-other-app@appspot.gserviceaccount.com`.
|
165
|
+
# * `group:{emailid}`: An email address that represents a Google group.
|
166
|
+
# For example, `admins@example.com`.
|
167
|
+
# * `domain:{domain}`: The G Suite domain (primary) that represents
|
168
|
+
# all the users of that domain. For example, `google.com` or
|
169
|
+
# `example.com`. Required.
|
170
|
+
#
|
171
|
+
def members= new_members
|
172
|
+
new_members = Array new_members
|
173
|
+
raise ArgumentError, "members is empty, must be provided" if new_members.empty?
|
174
|
+
@members = new_members
|
175
|
+
end
|
176
|
+
|
177
|
+
##
|
178
|
+
# Sets the condition for the binding.
|
179
|
+
#
|
180
|
+
# @param [Google::Cloud::Storage::Policy::Condition] new_condition The
|
181
|
+
# condition that is associated with this binding. NOTE: An unsatisfied
|
182
|
+
# condition will not allow user access via current binding. Different
|
183
|
+
# bindings, including their conditions, are examined independently.
|
184
|
+
# Optional.
|
185
|
+
# @overload condition=(title:, description: nil, expression:)
|
186
|
+
# @param [String] title Used to identify the condition. Required.
|
187
|
+
# @param [String] description Used to document the condition. Optional.
|
188
|
+
# @param [String] expression Defines an attribute-based logic
|
189
|
+
# expression using a subset of the Common Expression Language (CEL).
|
190
|
+
# The condition expression can contain multiple statements, each uses
|
191
|
+
# one attributes, and statements are combined using logic operators,
|
192
|
+
# following CEL language specification. Required.
|
193
|
+
#
|
194
|
+
def condition= new_condition
|
195
|
+
new_condition = Condition.new(**new_condition) if new_condition.is_a? Hash
|
196
|
+
if new_condition && !new_condition.is_a?(Condition)
|
197
|
+
raise ArgumentError, "expected Condition, not #{new_condition.inspect}"
|
198
|
+
end
|
199
|
+
@condition = new_condition
|
200
|
+
end
|
201
|
+
|
202
|
+
##
|
203
|
+
# @private
|
204
|
+
def <=> other
|
205
|
+
return nil unless other.is_a? Binding
|
206
|
+
|
207
|
+
ret = role <=> other.role
|
208
|
+
return ret unless ret.zero?
|
209
|
+
ret = members <=> other.members
|
210
|
+
return ret unless ret.zero?
|
211
|
+
condition&.to_gapi <=> other.condition&.to_gapi
|
212
|
+
end
|
213
|
+
|
214
|
+
##
|
215
|
+
# @private
|
216
|
+
def eql? other
|
217
|
+
role.eql?(other.role) &&
|
218
|
+
members.eql?(other.members) &&
|
219
|
+
condition&.to_gapi.eql?(other.condition&.to_gapi)
|
220
|
+
end
|
221
|
+
|
222
|
+
##
|
223
|
+
# @private
|
224
|
+
def hash
|
225
|
+
[
|
226
|
+
@role,
|
227
|
+
@members,
|
228
|
+
@condition&.to_gapi
|
229
|
+
].hash
|
230
|
+
end
|
231
|
+
|
232
|
+
##
|
233
|
+
# @private
|
234
|
+
def to_gapi
|
235
|
+
params = {
|
236
|
+
role: @role,
|
237
|
+
members: @members,
|
238
|
+
condition: @condition&.to_gapi
|
239
|
+
}.delete_if { |_, v| v.nil? }
|
240
|
+
Google::Apis::StorageV1::Policy::Binding.new(**params)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
# Copyright 2019 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
require "google/cloud/storage/policy/binding"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Storage
|
21
|
+
class Policy
|
22
|
+
##
|
23
|
+
# # Bindings
|
24
|
+
#
|
25
|
+
# Enumerable object for managing Cloud IAM bindings associated with
|
26
|
+
# a bucket.
|
27
|
+
#
|
28
|
+
# @see https://cloud.google.com/iam/docs/overview Cloud IAM Overview
|
29
|
+
#
|
30
|
+
# @example Updating a Policy from version 1 to version 3:
|
31
|
+
# require "google/cloud/storage"
|
32
|
+
#
|
33
|
+
# storage = Google::Cloud::Storage.new
|
34
|
+
# bucket = storage.bucket "my-bucket"
|
35
|
+
#
|
36
|
+
# bucket.uniform_bucket_level_access = true
|
37
|
+
#
|
38
|
+
# bucket.policy requested_policy_version: 3 do |p|
|
39
|
+
# p.version # the value is 1
|
40
|
+
# p.version = 3 # Must be explicitly set to opt-in to support for conditions.
|
41
|
+
#
|
42
|
+
# expr = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"
|
43
|
+
# p.bindings.insert({
|
44
|
+
# role: "roles/storage.admin",
|
45
|
+
# members: ["user:owner@example.com"],
|
46
|
+
# condition: {
|
47
|
+
# title: "my-condition",
|
48
|
+
# description: "description of condition",
|
49
|
+
# expression: expr
|
50
|
+
# }
|
51
|
+
# })
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
class Bindings
|
55
|
+
include Enumerable
|
56
|
+
|
57
|
+
##
|
58
|
+
# @private Creates a Bindings object.
|
59
|
+
def initialize
|
60
|
+
@bindings = []
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Adds a binding or bindings to the collection. The arguments may be
|
65
|
+
# {Google::Cloud::Storage::Policy::Binding} objects or equivalent hash
|
66
|
+
# objects that will be implicitly coerced to binding objects.
|
67
|
+
#
|
68
|
+
# @param [Google::Cloud::Storage::Policy::Binding, Hash] bindings One
|
69
|
+
# or more bindings to be added to the policy owning the collection.
|
70
|
+
# The arguments may be {Google::Cloud::Storage::Policy::Binding}
|
71
|
+
# objects or equivalent hash objects that will be implicitly coerced
|
72
|
+
# to binding objects.
|
73
|
+
#
|
74
|
+
# @return [Bindings] `self` for chaining.
|
75
|
+
#
|
76
|
+
# @example Updating a Policy from version 1 to version 3:
|
77
|
+
# require "google/cloud/storage"
|
78
|
+
#
|
79
|
+
# storage = Google::Cloud::Storage.new
|
80
|
+
# bucket = storage.bucket "my-bucket"
|
81
|
+
#
|
82
|
+
# bucket.uniform_bucket_level_access = true
|
83
|
+
#
|
84
|
+
# bucket.policy requested_policy_version: 3 do |p|
|
85
|
+
# p.version # the value is 1
|
86
|
+
# p.version = 3 # Must be explicitly set to opt-in to support for conditions.
|
87
|
+
#
|
88
|
+
# expr = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"
|
89
|
+
# p.bindings.insert({
|
90
|
+
# role: "roles/storage.admin",
|
91
|
+
# members: ["user:owner@example.com"],
|
92
|
+
# condition: {
|
93
|
+
# title: "my-condition",
|
94
|
+
# description: "description of condition",
|
95
|
+
# expression: expr
|
96
|
+
# }
|
97
|
+
# })
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
def insert *bindings
|
101
|
+
bindings = coerce_bindings(*bindings)
|
102
|
+
@bindings += bindings
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
##
|
107
|
+
# Deletes the binding or bindings from the collection that are equal to
|
108
|
+
# the arguments. The specification arguments may be
|
109
|
+
# {Google::Cloud::Storage::Policy::Binding} objects or equivalent hash
|
110
|
+
# objects that will be implicitly coerced to binding objects.
|
111
|
+
#
|
112
|
+
# @param [Google::Cloud::Storage::Policy::Binding, Hash] bindings One
|
113
|
+
# or more specifications for bindings to be removed from the
|
114
|
+
# collection. The arguments may be
|
115
|
+
# {Google::Cloud::Storage::Policy::Binding} objects or equivalent
|
116
|
+
# hash objects that will be implicitly coerced to binding objects.
|
117
|
+
#
|
118
|
+
# @return [Bindings] `self` for chaining.
|
119
|
+
#
|
120
|
+
# @example
|
121
|
+
# require "google/cloud/storage"
|
122
|
+
#
|
123
|
+
# storage = Google::Cloud::Storage.new
|
124
|
+
# bucket = storage.bucket "my-bucket"
|
125
|
+
#
|
126
|
+
# bucket.policy requested_policy_version: 3 do |p|
|
127
|
+
# expr = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"
|
128
|
+
# p.bindings.remove({
|
129
|
+
# role: "roles/storage.admin",
|
130
|
+
# members: ["user:owner@example.com"],
|
131
|
+
# condition: {
|
132
|
+
# title: "my-condition",
|
133
|
+
# description: "description of condition",
|
134
|
+
# expression: expr
|
135
|
+
# }
|
136
|
+
# })
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
def remove *bindings
|
140
|
+
bindings = coerce_bindings(*bindings)
|
141
|
+
@bindings -= bindings
|
142
|
+
self
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# Calls the block once for each binding in the collection, passing
|
147
|
+
# a {Google::Cloud::Storage::Policy::Binding} object as parameter. A
|
148
|
+
# {Google::Cloud::Storage::Policy::Binding} object is passed even
|
149
|
+
# when the arguments to {#insert} were hash objects.
|
150
|
+
#
|
151
|
+
# If no block is given, an enumerator is returned instead.
|
152
|
+
#
|
153
|
+
# @yield [binding] A binding in this bindings collection.
|
154
|
+
# @yieldparam [Google::Cloud::Storage::Policy::Binding] binding A
|
155
|
+
# binding object, even when the arguments to {#insert} were hash
|
156
|
+
# objects.
|
157
|
+
#
|
158
|
+
# @return [Enumerator]
|
159
|
+
#
|
160
|
+
# @example
|
161
|
+
# require "google/cloud/storage"
|
162
|
+
#
|
163
|
+
# storage = Google::Cloud::Storage.new
|
164
|
+
# bucket = storage.bucket "my-bucket"
|
165
|
+
#
|
166
|
+
# policy = bucket.policy requested_policy_version: 3
|
167
|
+
# policy.bindings.each do |binding|
|
168
|
+
# puts binding.role
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
def each &block
|
172
|
+
return enum_for :each unless block_given?
|
173
|
+
|
174
|
+
@bindings.each(&block)
|
175
|
+
end
|
176
|
+
|
177
|
+
##
|
178
|
+
# @private
|
179
|
+
def to_gapi
|
180
|
+
@bindings.map(&:to_gapi)
|
181
|
+
end
|
182
|
+
|
183
|
+
protected
|
184
|
+
|
185
|
+
def coerce_bindings *bindings
|
186
|
+
bindings.map do |binding|
|
187
|
+
binding = Binding.new(**binding) if binding.is_a? Hash
|
188
|
+
raise ArgumentError, "expected Binding, not #{binding.inspect}" unless binding.is_a? Binding
|
189
|
+
binding
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# Copyright 2019 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
module Google
|
17
|
+
module Cloud
|
18
|
+
module Storage
|
19
|
+
class Policy
|
20
|
+
##
|
21
|
+
# # Condition
|
22
|
+
#
|
23
|
+
# Value object accepting an attribute-based logic expression based on a
|
24
|
+
# subset of the Common Expression Language (CEL).
|
25
|
+
#
|
26
|
+
# @see https://cloud.google.com/iam/docs/conditions-overview Cloud IAM
|
27
|
+
# policies with conditions
|
28
|
+
#
|
29
|
+
# @attr [String] title Used to identify the condition. Required.
|
30
|
+
# @attr [String] description Used to document the condition. Optional.
|
31
|
+
# @attr [String] expression Defines an attribute-based logic
|
32
|
+
# expression using a subset of the Common Expression Language (CEL).
|
33
|
+
# The condition expression can contain multiple statements, each uses
|
34
|
+
# one attributes, and statements are combined using logic operators,
|
35
|
+
# following CEL language specification. Required.
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# require "google/cloud/storage"
|
39
|
+
#
|
40
|
+
# storage = Google::Cloud::Storage.new
|
41
|
+
# bucket = storage.bucket "my-bucket"
|
42
|
+
#
|
43
|
+
# policy = bucket.policy requested_policy_version: 3
|
44
|
+
# policy.bindings.each do |binding|
|
45
|
+
# puts binding.condition.title if binding.condition
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# @example Updating a Policy from version 1 to version 3 by adding a condition:
|
49
|
+
# require "google/cloud/storage"
|
50
|
+
#
|
51
|
+
# storage = Google::Cloud::Storage.new
|
52
|
+
# bucket = storage.bucket "my-bucket"
|
53
|
+
#
|
54
|
+
# bucket.uniform_bucket_level_access = true
|
55
|
+
#
|
56
|
+
# bucket.policy requested_policy_version: 3 do |p|
|
57
|
+
# p.version # the value is 1
|
58
|
+
# p.version = 3 # Must be explicitly set to opt-in to support for conditions.
|
59
|
+
#
|
60
|
+
# expr = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"
|
61
|
+
# p.bindings.insert({
|
62
|
+
# role: "roles/storage.admin",
|
63
|
+
# members: ["user:owner@example.com"],
|
64
|
+
# condition: {
|
65
|
+
# title: "my-condition",
|
66
|
+
# description: "description of condition",
|
67
|
+
# expression: expr
|
68
|
+
# }
|
69
|
+
# })
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
class Condition
|
73
|
+
attr_reader :title
|
74
|
+
attr_reader :description
|
75
|
+
attr_reader :expression
|
76
|
+
|
77
|
+
##
|
78
|
+
# Creates a Condition object.
|
79
|
+
#
|
80
|
+
# @param [String] title Used to identify the condition. Required.
|
81
|
+
# @param [String] description Used to document the condition. Optional.
|
82
|
+
# @param [String] expression Defines an attribute-based logic
|
83
|
+
# expression using a subset of the Common Expression Language (CEL).
|
84
|
+
# The condition expression can contain multiple statements, each uses
|
85
|
+
# one attributes, and statements are combined using logic operators,
|
86
|
+
# following CEL language specification. Required.
|
87
|
+
#
|
88
|
+
def initialize title:, expression:, description: nil
|
89
|
+
@title = String title
|
90
|
+
@description = String description
|
91
|
+
@expression = String expression
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# The title used to identify the condition. Required.
|
96
|
+
#
|
97
|
+
# @param [String] new_title The new title.
|
98
|
+
#
|
99
|
+
def title= new_title
|
100
|
+
@title = String new_title
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# The description to document the condition. Optional.
|
105
|
+
#
|
106
|
+
# @param [String] new_description The new description.
|
107
|
+
#
|
108
|
+
def description= new_description
|
109
|
+
@description = String new_description
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# An attribute-based logic expression using a subset of the Common
|
114
|
+
# Expression Language (CEL). The condition expression can contain
|
115
|
+
# multiple statements, each uses one attributes, and statements are
|
116
|
+
# combined using logic operators, following CEL language
|
117
|
+
# specification. Required.
|
118
|
+
#
|
119
|
+
# @see https://cloud.google.com/iam/docs/conditions-overview CEL for conditions
|
120
|
+
#
|
121
|
+
# @param [String] new_expression The new expression.
|
122
|
+
#
|
123
|
+
def expression= new_expression
|
124
|
+
@expression = String new_expression
|
125
|
+
end
|
126
|
+
|
127
|
+
def to_gapi
|
128
|
+
{
|
129
|
+
title: @title,
|
130
|
+
description: @description,
|
131
|
+
expression: @expression
|
132
|
+
}.delete_if { |_, v| v.nil? }
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|