class Rbeapi::Api::AaaGroups

The AaaGroups class manages the server groups on an EOS node.

Constants

DEFAULT_RADIUS_ACCT_PORT
DEFAULT_RADIUS_AUTH_PORT
RADIUS_GROUP_SERVER

Regular expression that parses the radius servers from the aaa group server radius configuration block.

TACACS_GROUP_SERVER

Regular expression that parses the tacacs servers from the aaa group server tacacs+ configuration block.

Public Instance Methods

add_radius_server(name, server, opts = {}) click to toggle source

add_radius_server adds a new radius server to the nodes current configuration. If the server already exists in the specified group name this method will still return successfully.

@since eos_version 4.13.7M

commmands

aaa group server radius <name>
server <server> [acct-port <acct_port>] [auth-port <auth_port>]
                [vrf <vrf>]

@param name [String] The name of the aaa group server to add the new

server configuration to.

@param server [String] The IP address or host name of the server to

add to the configuration.

@param opts [Hash] Optional configuration parameters.

@return [Boolean] Returns true if the commands complete successfully.

# File lib/rbeapi/api/aaa.rb, line 378
def add_radius_server(name, server, opts = {})
  # order of command options matter here!
  server = "server #{server} "
  server << "auth-port #{opts[:auth_port]} " if opts[:auth_port]
  server << "acct-port #{opts[:acct_port]} " if opts[:acct_port]
  server << "vrf #{opts[:vrf]}" if opts[:vrf]
  configure ["aaa group server radius #{name}", server, 'exit']
end
add_server(name, server, opts = {}) click to toggle source

add_server adds a new server to the specified aaa server group. If the server is already configured in the list of servers, this method will still return successfully.

@see add_radius_server @see add_tacacs_server

@param name [String] The name of the aaa group server to add the new

server configuration to.

@param server [String] The IP address or host name of the server to

add to the configuration.

@param opts [Hash] Optional configuration parameters.

@return [Boolean] Returns true if the commands complete successfully.

# File lib/rbeapi/api/aaa.rb, line 347
def add_server(name, server, opts = {})
  type = find_type(name)
  return false unless type
  case type
  when 'radius' then add_radius_server(name, server, opts)
  when 'tacacs+' then add_tacacs_server(name, server, opts)
  else return false
  end
end
add_tacacs_server(name, server, opts = {}) click to toggle source

add_tacacs_server adds a new tacacs server to the nodes current configuration. If the server already exists in the specified group name this method will still return successfully.

@since eos_version 4.13.7M

commmands

aaa group server tacacs+ <name>
server <server> [acct-port <acct_port>] [auth-port <auth_port>]
                [vrf <vrf>]

@param name [String] The name of the aaa group server to add the new

server configuration to.

@param server [String] The IP address or host name of the server to

add to the configuration.

@param opts [Hash] Optional configuration parameters.

@return [Boolean] Returns true if the commands complete successfully.

# File lib/rbeapi/api/aaa.rb, line 408
def add_tacacs_server(name, server, opts = {})
  # order of command options matter here!
  server = "server #{server} "
  server << "vrf #{opts[:vrf]} "    if opts[:vrf]
  server << "port #{opts[:port]} "  if opts[:port]
  configure ["aaa group server tacacs+ #{name}", server, 'exit']
end
create(name, type) click to toggle source

create adds a new aaa group server to the nodes current configuration. If the specified name and type are already created then this method will return successfully. If the name is configured but the type is different, this method will not return successfully (returns false).

@since eos_version 4.13.7M

Commands

aaa group server <type> <name>

@param name [String] The name of the aaa group server to create in the

nodes running configuration

@param type [String] The type of aaa group server to create in the

nodes running configuration. Valid values include 'radius' or
'tacacs+'

@return [Boolean] returns true if the commands complete successfully

# File lib/rbeapi/api/aaa.rb, line 277
def create(name, type)
  configure ["aaa group server #{type} #{name}", 'exit']
end
delete(name) click to toggle source

delete removes a current aaa server group from the nodes current configuration. This method will automatically determine the server group type based on the name. If the name is not configured in the nodes current configuration, this method will return successfully.

@since eos_version 4.13.7M

Commands

no aaa group server [radius | tacacs+] <name>

@param name [String] The name of the aaa group server to create in the

nodes running configuration.

@return [Boolean] Returns true if the commands complete successfully.

# File lib/rbeapi/api/aaa.rb, line 296
def delete(name)
  type = find_type(name)
  return true unless type
  configure "no aaa group server #{type} #{name}"
end
get(name) click to toggle source

get returns the aaa server group resource hash that describes the current configuration for the specified server group name.

@example

{
  type: <string>,
  servers: <array>
}

@param name [String] The server group name to return from the nodes

current running configuration. If the name is not configured a nil
object is returned.

@return [nil, Hash<Symbol, Object>] Returns the resource hash for the

specified name. If the name does not exist, a nil object is returned.
# File lib/rbeapi/api/aaa.rb, line 114
def get(name)
  block = get_block("aaa group server ([^\s]+) #{name}")
  return nil unless block
  response = {}
  response.merge!(parse_type(block))
  response.merge!(parse_servers(block, response[:type]))
  response
end
getall() click to toggle source

getall returns a aaa server groups hash.

@example

{
  <name>: {
    type: <string>,
    servers: <array>
  },
  <name>: {
    type: <string>,
    servers: <array>
  }
}

@return [Hash<Symbol, Object>] Returns the resource hashes for

configured aaa groups. If none exist, a nil object is returned.
# File lib/rbeapi/api/aaa.rb, line 140
def getall
  cfg = config.scan(/aaa group server (?:radius|tacacs\+) (.+)$/)
  cfg.each_with_object({}) do |name, hsh|
    values = get(name.first)
    hsh[name.first] = values if values
  end
end
parse_tacacs_server(config) click to toggle source

parse_tacacs_server scans the provided configuration block and returns the list of configured servers. The configuration block is expected to be a tacacs configuration block. If there are no servers configured for the group the servers value will return an empty array.

@api private

@param config [String] The aaa server group block configuration for the

group name to parse.

@return [Hash<Symbol, Object>] Resource hash attribute.

# File lib/rbeapi/api/aaa.rb, line 226
def parse_tacacs_server(config)
  values = config.scan(TACACS_GROUP_SERVER).map do |(name, vrf, port)|
    {
      name: name,
      vrf: vrf,
      port: port
    }
  end
  { servers: values }
end
remove_server(name, server, opts = {}) click to toggle source

remove_server deletes an existing server from the specified aaa server group. If the specified server is not configured in the specified server group, this method will still return true.

eos_version 4.13.7M

Commands

aaa group server [radius | tacacs+] <name>
no server <server>

@param name [String] The name of the aaa group server to remove.

@param server [String] The IP address or host name of the server.

@param opts [Hash] Optional configuration parameters.

@return [Boolean] returns true if the commands complete successfully.

# File lib/rbeapi/api/aaa.rb, line 434
def remove_server(name, server, opts = {})
  type = find_type(name)
  return false unless type
  server = "no server #{server} "
  server << "vrf #{opts[:vrf]}" if opts[:vrf]
  configure ["aaa group server #{type} #{name}", server, 'exit']
end
set_servers(name, servers) click to toggle source

set_servers configures the set of servers for a specified aaa server group. This is an atomic operation that first removes all current servers and then adds the new servers back. If any of the servers failes to be removed or added, this method will return unsuccessfully.

@see remove_server @see add_server

@param name [String] The name of the aaa group server to add the new

server configuration to.

@param servers [String] The IP address or host name of the server to

add to the configuration

@return [Boolean] Returns true if the commands complete successfully

# File lib/rbeapi/api/aaa.rb, line 318
def set_servers(name, servers)
  current = get(name)
  current[:servers].each do |srv|
    return false unless remove_server(name, srv)
  end
  servers.each do |srv|
    hostname = srv[:name]
    return false unless add_server(name, hostname, srv)
  end
  true
end

Private Instance Methods

find_type(name) click to toggle source

find_type is a utility method to find the type of aaa server group for the specified name. This method will scan the current running configuration on the node and return the server group type as either 'radius' or 'tacacs+'. If the server group is not configured, then nil will be returned.

@api private

@param name [String] The aaa server group name to find in the config

and return the type value for.

@return [nil, String] Returns either the type name as 'radius' or

'tacacs+' or nil if the server group is not configured.
# File lib/rbeapi/api/aaa.rb, line 252
def find_type(name)
  mdata = /aaa group server ([^\s]+) #{name}/.match(config)
  return mdata[1] if mdata
end
parse_radius_server(config) click to toggle source

parse_radius_server scans the provide configuration block and returns the list of servers configured. The configuration block is expected to be a radius configuration block. If there are no servers configured for the group the servers value will return an empty array.

@api private

@param config [String] The aaa server group block configuration for the

group name to parse

@return [Hash<Symbol, Object>] resource hash attribute

# File lib/rbeapi/api/aaa.rb, line 202
def parse_radius_server(config)
  values = config.scan(RADIUS_GROUP_SERVER).map do |(name, auth, acct)|
    {
      name: name,
      auth_port: auth || DEFAULT_RADIUS_AUTH_PORT,
      acct_port: acct || DEFAULT_RADIUS_ACCT_PORT
    }
  end
  { servers: values }
end
parse_servers(config, type) click to toggle source

parse_servers scans the specified configuraiton block and returns the list of servers configured for the group. If there are no servers configured for the group the servers value will return an empty array.

@api private

@see parse_radius_server @see parse_tacacs_server

@param config [String] The aaa server group block configuration for the

group name to parse.

@param type [String] The aaa server block type. Valid values are

either radius or tacacs+.

@return [Hash<Symbol, Object>] Resource hash attribute

# File lib/rbeapi/api/aaa.rb, line 182
def parse_servers(config, type)
  case type
  when 'radius' then parse_radius_server(config)
  when 'tacacs+' then parse_tacacs_server(config)
  end
end
parse_type(config) click to toggle source

parse_type scans the specified configuration block and returns the server group type as either 'tacacs' or 'radius'. The type value is expected to always be present in the config.

@api private

@param config [String] The aaa server group block configuration for the

group name to parse.

@return [Hash<Symbol, Object>] Resource hash attribute.

# File lib/rbeapi/api/aaa.rb, line 159
def parse_type(config)
  value = config.scan(/aaa group server ([^\s]+)/).first
  { type: value.first }
end