Class: Rbeapi::Api::Users
Overview
The Users class provides configuration of local user resources for an EOS node.
Instance Attribute Summary
Attributes inherited from Entity
Instance Method Summary collapse
-
#create(name, opts = {}) ⇒ Boolean
create will create a new user name resource in the nodes current configuration with the specified user name.
-
#default(name) ⇒ Boolean
default will configure the user name using the default keyword.
-
#delete(name) ⇒ Boolean
delete will delete an existing user name from the nodes current running configuration.
-
#get(name) ⇒ nil, Hash<Symbol, Object>
get returns the local user configuration.
-
#getall ⇒ Hash<Symbol, Object>
getall returns a collection of user resource hashes from the nodes running configuration.
-
#initialize(node) ⇒ Users
constructor
A new instance of Users.
-
#set_privilege(name, opts = {}) ⇒ Boolean
set_privilege configures the user privilege value for the specified user name in the nodes running configuration.
-
#set_role(name, opts = {}) ⇒ Boolean
set_role configures the user role value for the specified user name in the nodes running configuration.
-
#set_sshkey(name, opts = {}) ⇒ Boolean
set_sshkey configures the user sshkey value for the specified user name in the nodes running configuration.
Methods inherited from Entity
#command_builder, #configure, #configure_interface, #get_block, instance
Constructor Details
#initialize(node) ⇒ Users
Returns a new instance of Users.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rbeapi/api/users.rb', line 44 def initialize(node) super(node) # The regex used here parses the running configuration to find all # username entries. There is extra logic in the regular expression # to store the username as 'user' and then creates a back reference # to find a following configuration line that might contain the # users sshkey. @users_re = Regexp.new(/^username\s+(?<user>[^\s]+)\s+ privilege\s+(?<priv>\d+) (\s+role\s+(?<role>\S+))? (?:\s+(?<nopassword>(nopassword)))? (\s+secret\s+(?<encryption>0|5|7|sha512)\s+ (?<secret>\S+))?.*$\n (username\s+\k<user>\s+ sshkey\s+(?<sshkey>.*)$)?/x) @encryption_map = { 'cleartext' => '0', 'md5' => '5', 'sha512' => 'sha512' } end |
Instance Method Details
#create(name, opts = {}) ⇒ Boolean
create will create a new user name resource in the nodes current configuration with the specified user name. Creating users require either a secret (password) or the nopassword keyword to be specified. Optional parameters can be passed in to initialize user name specific settings.
Commands
username <name> nopassword privilege <value> role <value>
username <name> secret [0,5,sha512] <secret> ...
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rbeapi/api/users.rb', line 211 def create(name, opts = {}) cmd = "username #{name}" cmd << " privilege #{opts[:privilege]}" if opts[:privilege] cmd << " role #{opts[:role]}" if opts[:role] if opts[:nopassword] == :true cmd << ' nopassword' else # Map the encryption value if set, if there is no mapping then # just return the value. enc = opts.fetch(:encryption, 'cleartext') unless @encryption_map[enc] raise ArgumentError, "invalid encryption value: #{enc}" end enc = @encryption_map[enc] unless opts[:secret] raise ArgumentError, 'secret must be specified if nopassword is false' end cmd << " secret #{enc} #{opts[:secret]}" end cmds = [cmd] cmds << "username #{name} sshkey #{opts[:sshkey]}" if opts[:sshkey] configure(cmds) end |
#default(name) ⇒ Boolean
default will configure the user name using the default keyword. This command has the same effect as deleting the user name from the nodes running configuration.
Commands
default username <name>
268 269 270 |
# File 'lib/rbeapi/api/users.rb', line 268 def default(name) configure("default username #{name}") end |
#delete(name) ⇒ Boolean
delete will delete an existing user name from the nodes current running configuration. If the delete method is called and the user name does not exist, this method will succeed.
Commands
no username <name>
250 251 252 |
# File 'lib/rbeapi/api/users.rb', line 250 def delete(name) configure("no username #{name}") end |
#get(name) ⇒ nil, Hash<Symbol, Object>
get returns the local user configuration.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rbeapi/api/users.rb', line 85 def get(name) # The regex used here parses the running configuration to find one # username entry. user_re = Regexp.new(/^username\s+(?<user>#{name})\s+ privilege\s+(?<priv>\d+) (\s+role\s+(?<role>\S+))? (?:\s+(?<nopassword>(nopassword)))? (\s+secret\s+(?<encryption>0|5|7|sha512)\s+ (?<secret>\S+))?.*$\n (username\s+#{name}\s+ sshkey\s+(?<sshkey>.*)$)?/x) user = config.scan(user_re) return nil unless user && user[0] parse_user_entry(user[0]) end |
#getall ⇒ Hash<Symbol, Object>
getall returns a collection of user resource hashes from the nodes running configuration. The user resource collection hash is keyed by the unique user name.
133 134 135 136 137 138 139 140 |
# File 'lib/rbeapi/api/users.rb', line 133 def getall entries = config.scan(@users_re) response = {} entries.each do |user| response[user[0]] = parse_user_entry(user) end response end |
#set_privilege(name, opts = {}) ⇒ Boolean
set_privilege configures the user privilege value for the specified user name in the nodes running configuration. If enable is false in the opts keyword Hash then the name value is negated using the no keyword. If the default keyword is set to true, then the privilege value is defaulted using the default keyword. The default keyword takes precedence over the enable keyword
Commands
username <name> privilege <value>
no username <name> privilege <value>
default username <name> privilege <value>
301 302 303 |
# File 'lib/rbeapi/api/users.rb', line 301 def set_privilege(name, opts = {}) configure(command_builder("username #{name} privilege", opts)) end |
#set_role(name, opts = {}) ⇒ Boolean
set_role configures the user role value for the specified user name in the nodes running configuration. If enable is false in the opts keyword Hash then the name value is negated using the no keyword. If the default keyword is set to true, then the role value is defaulted using the default keyword. The default keyword takes precedence over the enable keyword
Commands
username <name> role <value>
no username <name> role <value>
default username <name> role <value>
334 335 336 |
# File 'lib/rbeapi/api/users.rb', line 334 def set_role(name, opts = {}) configure(command_builder("username #{name} role", opts)) end |
#set_sshkey(name, opts = {}) ⇒ Boolean
set_sshkey configures the user sshkey value for the specified user name in the nodes running configuration. If enable is false in the opts keyword Hash then the name value is negated using the no keyword. If the default keyword is set to true, then the sshkey value is defaulted using the default keyword. The default keyword takes precedence over the enable keyword.
Commands
username <name> sshkey <value>
no username <name> sshkey <value>
default username <name> sshkey <value>
367 368 369 |
# File 'lib/rbeapi/api/users.rb', line 367 def set_sshkey(name, opts = {}) configure(command_builder("username #{name} sshkey", opts)) end |