Class: OpenNebula::Acl

Inherits:
PoolElement show all
Defined in:
lib/opennebula/acl.rb

Overview

Abstract rules of the type USER RESOURCE RIGHTS which are:

USER      -> #<num>
             @<num>
             ALL
RESOURCE  -> + separated list and "/{#,@,%}<num>|ALL"
             VM
             HOST
             NET
             IMAGE
             USER
             TEMPLATE
             GROUP
             DATASTORE
             CLUSTER
             DOCUMENT
             ZONE
             SECGROUP
             VDC
             VROUTER
             MARKETPLACE
             MARKETPLACEAPP
             VMGROUP
RIGHTS    -> + separated list
             USE
             MANAGE
             ADMIN
             CREATE

Constant Summary collapse

USERS =
{
    "UID"           => 0x100000000,
    "GID"           => 0x200000000,
    "ALL"           => 0x400000000,
    "CLUSTER"       => 0x800000000
}
RESOURCES =
{
    "VM"            =>     0x1000000000,
    "HOST"          =>     0x2000000000,
    "NET"           =>     0x4000000000,
    "IMAGE"         =>     0x8000000000,
    "USER"          =>    0x10000000000,
    "TEMPLATE"      =>    0x20000000000,
    "GROUP"         =>    0x40000000000,
    "DATASTORE"     =>   0x100000000000,
    "CLUSTER"       =>   0x200000000000,
    "DOCUMENT"      =>   0x400000000000,
    "ZONE"          =>   0x800000000000,
    "SECGROUP"      =>  0x1000000000000,
    "VDC"           =>  0x2000000000000,
    "VROUTER"       =>  0x4000000000000,
    "MARKETPLACE"   =>  0x8000000000000,
    "MARKETPLACEAPP"=> 0x10000000000000,
    "VMGROUP"       => 0x20000000000000,
    "VNTEMPLATE"    => 0x40000000000000,
    "BACKUPJOB"     =>0x100000000000000
}
RIGHTS =
{
    "USE"           => 0x1,  # Auth. to use an object
    "MANAGE"        => 0x2,  # Auth. to perform management actions
    "ADMIN"         => 0x4,  # Auth. to perform administrative actions
    "CREATE"        => 0x8   # Auth. to create an object
}

Instance Attribute Summary

Attributes inherited from PoolElement

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, new_with_id, #replace, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #set_content, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

#initialize(xml, client) ⇒ Acl

Constructor

Parameters:

  • xml (String)

    must be an xml built with build_xml

  • client (Client)

    represents an XML-RPC connection



93
94
95
# File 'lib/opennebula/acl.rb', line 93

def initialize(xml, client)
    super(xml,client)
end

Class Method Details

.build_xml(pe_id = nil) ⇒ String

Creates an empty XML representation. It contains the id, if it is specified.

Parameters:

  • pe_id (Integer) (defaults to: nil)

    rule ID

Returns:

  • (String)

    an empty XML representation



103
104
105
106
107
108
109
110
111
# File 'lib/opennebula/acl.rb', line 103

def self.build_xml(pe_id=nil)
    if pe_id
        acl_xml = "<ACL><ID>#{pe_id}</ID></ACL>"
    else
        acl_xml = "<ACL></ACL>"
    end

    XMLElement.build_xml(acl_xml,'ACL')
end

.calculate_ids(id_str) ⇒ Integer

Calculates the numeric value for a String containing an individual (#<id>), group (@<id>) or all (*) ID component

Parameters:

  • id_str (String)

    Rule Id string

Returns:

  • (Integer)

    the numeric value for the given id_str



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/opennebula/acl.rb', line 282

def self.calculate_ids(id_str)
    raise "ID string '#{id_str}' malformed" if
        !id_str.match(/^([\#@\%]\d+|\*)$/)

    value = 0

    case id_str[0..0]
        when "#"
            value = USERS["UID"]
            users_value = id_str[1..-1].to_i + value

        when "@"
            value = USERS["GID"]
            users_value = id_str[1..-1].to_i + value

        when "*"
            users_value = USERS["ALL"]

        when "%"
            value = USERS["CLUSTER"]
            users_value = id_str[1..-1].to_i + value
    end

    return users_value
end

.parse_resources(resources) ⇒ String

Converts a resources string to a hex. number

Parameters:

  • resources (String)

    Resources component string

Returns:

  • (String)

    A string containing a hex number



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/opennebula/acl.rb', line 217

def self.parse_resources(resources)
    begin
        ret = 0
        resources = resources.split("/")

        if resources.size != 2
            raise "Resource '#{resources}' malformed"
        end

        resources[0].split("+").each{ |resource|
            if !RESOURCES[resource.upcase]
                raise "Resource '#{resource}' does not exist"
            end
            ret += RESOURCES[resource.upcase]
        }

        ret += calculate_ids(resources[1])

        return ret.to_i.to_s(16)
    rescue Exception  => e
        return OpenNebula::Error.new(e.message)
    end
end

.parse_rights(rights) ⇒ String

Converts a rights string to a hex. number

Parameters:

  • rights (String)

    Rights component string

Returns:

  • (String)

    A string containing a hex number



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/opennebula/acl.rb', line 246

def self.parse_rights(rights)
    begin
        ret = 0
        rights = rights.split("+")

        rights.each{ |right|
            raise "Right '#{right}' does not exist" if !RIGHTS[right.upcase]

            ret += RIGHTS[right.upcase]
        }

        return ret.to_i.to_s(16)
    rescue Exception  => e
        return OpenNebula::Error.new(e.message)
    end
end

.parse_rule(rule_str) ⇒ Array

Parses a rule string, e.g. “#5 HOST+VM/@12 INFO+CREATE+DELETE”

or OpenNebula::Error objects

Parameters:

  • rule_str (String)

    an ACL rule in string format

Returns:

  • (Array)

    an Array containing 3 strings (hex 64b numbers),



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/opennebula/acl.rb', line 162

def self.parse_rule(rule_str)
    ret = Array.new

    rule_str = rule_str.split(" ")

    if rule_str.length != 3 && rule_str.length != 4
        return OpenNebula::Error.new(
            "String needs three components: User, Resource, Rights")
    end

    ret << parse_users(rule_str[0])
    ret << parse_resources(rule_str[1])
    ret << parse_rights(rule_str[2])

    if rule_str.length > 3
        ret << parse_zone(rule_str[3])
    end

    errors=ret.map do |arg|
        if OpenNebula.is_error?(arg)
            arg.message
        else
            nil
        end
    end

    errors.compact!

    if errors.length>0
        return OpenNebula::Error.new(errors.join(', '))
    end

    return ret
end

.parse_users(users) ⇒ String

Converts a string in the form [#<id>, @<id>, *] to a hex. number

Parameters:

  • users (String)

    Users component string

Returns:

  • (String)

    A string containing a hex number



204
205
206
207
208
209
210
# File 'lib/opennebula/acl.rb', line 204

def self.parse_users(users)
   begin
       return calculate_ids(users).to_i.to_s(16)
   rescue Exception  => e
       return OpenNebula::Error.new(e.message)
   end
end

.parse_zone(zone) ⇒ String

Converts a string in the form [#<id>, *] to a hex. number

Parameters:

  • zone (String)

    Zone component string

Returns:

  • (String)

    A string containing a hex number



268
269
270
271
272
273
274
# File 'lib/opennebula/acl.rb', line 268

def self.parse_zone(zone)
   begin
       return calculate_ids(zone).to_i.to_s(16)
   rescue Exception  => e
       return OpenNebula::Error.new(e.message)
   end
end

Instance Method Details

#allocate(user, resource, rights, zone = '') ⇒ nil, OpenNebula::Error

Creates a new ACL rule.

Parameters:

  • user (String)

    A string containing a hex number, e.g. 0x100000001

  • resource (String)

    A string containing a hex number, e.g. 0x2100000001

  • rights (String)

    A string containing a hex number, e.g. 0x10

  • zone (String) (defaults to: '')

    A string containing a hex number, e.g. 0x100000001

Returns:



126
127
128
129
130
131
132
133
134
# File 'lib/opennebula/acl.rb', line 126

def allocate(user, resource, rights, zone='')
    require 'opennebula/acl_pool'

    return super( AclPool::ACL_POOL_METHODS[:addrule],
                user,
                resource,
                rights,
                zone )
end

#deletenil, OpenNebula::Error

Deletes the Acl rule

Returns:



140
141
142
143
144
# File 'lib/opennebula/acl.rb', line 140

def delete()
    require 'opennebula/acl_pool'

    super(AclPool::ACL_POOL_METHODS[:delrule])
end

#infonil Also known as: info!

Does nothing, individual ACL rules info can’t be retrieved from OpenNebula

Returns:

  • (nil)

    nil



150
151
152
# File 'lib/opennebula/acl.rb', line 150

def info()
    return nil
end