Class: Rbeapi::Api::Users

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/users.rb

Overview

The Users class provides configuration of local user resources for an EOS node.

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

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> ...

Parameters:

  • name (String)

    The name of the user to create.

  • opts (hash) (defaults to: {})

    Optional keyword arguments.

Options Hash (opts):

  • nopassword (Boolean)

    Configures the user to be able to authenticate without a password challenge.

  • secret (String)

    The secret (password) to assign to this user.

  • encryption (String)

    Specifies how the secret is encoded. Valid values are “cleartext”, “md5”, “sha512”. The default is “cleartext”.

  • privilege (String)

    The privilege value to assign to the user.

  • role (String)

    The role value to assign to the user.

  • sshkey (String)

    The sshkey value to assign to the user.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



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>

Parameters:

  • name (String)

    The user name to default in the nodes configuration.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.

Since:

  • eos_version 4.13.7M



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>

Parameters:

  • name (String)

    The user name to delete from the node.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



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.

Examples:

{
  name: <string>,
  privilege: <integer>,
  role: <string>,
  nopassword: <boolean>,
  encryption: <'cleartext', 'md5', 'sha512'>
  secret: <string>,
  sshkey: <string>
}

Parameters:

  • name (String)

    The user name to return a resource for from the nodes configuration

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the user resource as a Hash. If the specified user name is not found in the nodes current configuration a nil object is returned.



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

#getallHash<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.

Examples:

[
  <username>: {
    name: <string>,
    privilege: <integer>,
    role: <string>,
    nopassword: <boolean>,
    encryption: <'cleartext', 'md5', 'sha512'>
    secret: <string>,
    sshkey: <string>
  },
  <username>: {
    name: <string>,
    privilege: <integer>,
    role: <string>,
    nopassword: <boolean>,
    encryption: <'cleartext', 'md5', 'sha512'>
    secret: <string>,
    sshkey: <string>
  },
  ...
]

Returns:

  • (Hash<Symbol, Object>)

    Returns a hash that represents the entire user collection from the nodes running configuration. If there are no user names configured, this method will return an empty

    hash.
    


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>

Parameters:

  • name (String)

    The user name to default in the nodes configuration.

  • opts (Hash) (defaults to: {})

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    The privilege value to assign to the user.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the user privilege value using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



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>

Parameters:

  • name (String)

    The user name to default in the nodes configuration.

  • opts (Hash) (defaults to: {})

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    The role value to assign to the user.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the user role value using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



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>

Parameters:

  • name (String)

    The user name to default in the nodes configuration.

  • opts (Hash) (defaults to: {})

    Optional keyword arguments

Options Hash (opts):

  • value (String)

    The sshkey value to assign to the user

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the user sshkey value using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



367
368
369
# File 'lib/rbeapi/api/users.rb', line 367

def set_sshkey(name, opts = {})
  configure(command_builder("username #{name} sshkey", opts))
end