Module: Grafana::Auth

Included in:
Client
Defined in:
lib/grafana/auth.rb

Overview

abstract base class for authentication API

grafana.com/docs/grafana/latest/http_api/auth/

Token

- Currently you can authenticate via an API Token or via a Session cookie (acquired using regular login or oauth).

Basic Auth

- If basic auth is enabled (it is enabled by default) you can authenticate your HTTP request via standard
  basic auth. Basic auth will also authenticate LDAP users.

Instance Method Summary collapse

Instance Method Details

#api_key(api_id) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/grafana/auth.rb', line 31

def api_key( api_id )

  if( api_id.is_a?(String) && api_id.is_a?(Integer) )
    raise ArgumentError.new(format('wrong type. API token \'api_id\' must be an String (for an API name) or an Integer (for an API Id), given \'%s\'', api_id.class.to_s))
  end
  raise ArgumentError.new('missing \'api_id\'') if( api_id.size.zero? )

  if(api_id.is_a?(String))
    keys  = api_keys
    keys  = JSON.parse(keys) if(keys.is_a?(String))

#         logger.debug(keys)

    status = keys.dig('status')
    return keys if( status != 200 )

    u = keys.dig('message').detect { |v| v['id'] == api_id || v['name'] == api_id }

#         logger.debug(u)

    return { 'status' => 404, 'message' => format( 'No API token \'%s\' found', api_id ) } if( u.nil? )

    # api_id = u.dig('id') unless(u.nil?)
  end

  { 'status' => 200, 'message' => u }

end

#api_keysObject

Auth HTTP resources / actions Api Keys

GET /api/auth/keys



21
22
23
24
25
26
27
28
# File 'lib/grafana/auth.rb', line 21

def api_keys

  endpoint = '/api/auth/keys'

  @logger.debug("Attempting to get all existing api keys (GET #{endpoint})") if @debug

  get( endpoint )
end

#create_api_key(params) ⇒ Hash

Create API Key

POST /api/auth/keys grafana.com/docs/grafana/latest/http_api/auth/#create-api-key

@example:

Parameters:

Options Hash (params):

  • name (String)

    The key name - (required)

  • role (String)

    Sets the access level/Grafana Role for the key. Can be one of the following values: Viewer, Editor or Admin. - (required)

  • seconds_to_live (Integer)

    Sets the key expiration in seconds. It is optional. If it is a positive number an expiration date for the key is set. If it is null, zero or is omitted completely (unless api_key_max_seconds_to_live configuration option is set) the key will never expire.

Returns:

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/grafana/auth.rb', line 78

def create_api_key( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )

  name            = validate( params, required: true, var: 'name' )
  role            = validate( params, required: true, var: 'role' )
  seconds_to_live = validate( params, required: false, var: 'seconds_to_live', type: Integer )

  valid_roles     = %w[Viewer Editor Admin]

  # https://stackoverflow.com/questions/9333952/case-insensitive-arrayinclude?answertab=votes#tab-top
  # Do this once, or each time the array changes
  downcased = Set.new valid_roles.map(&:downcase)
  unless( downcased.include?( role.downcase ) )
    return {
      'status' => 404,
      'login_or_email' => ,
      'role' => role,
      'message' => format( 'wrong role. Role must be one of %s, given \'%s\'', valid_roles.join(', '), role )
    }
  end

  seconds_to_live = 86_400 if seconds_to_live.nil?

  endpoint = '/api/auth/keys'

  data = {
    name: name,
    role: role,
    secondsToLive: seconds_to_live
  }

  data.reject!{ |_k, v| v.nil? }

  payload = data.deep_string_keys
  # payload = existing_ds.merge(payload).deep_symbolize_keys

  @logger.debug("create API token (POST #{endpoint})") if @debug

  post(endpoint, payload.to_json)

end

#delete_api_key(key_id) ⇒ Object

Delete API Key

DELETE /api/auth/keys/:id

Raises:

  • (ArgumentError)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/grafana/auth.rb', line 125

def delete_api_key( key_id )

  if( key_id.is_a?(String) && key_id.is_a?(Integer) )
    raise ArgumentError.new(format('wrong type. \'key_id\' must be an String (for an API Key name) or an Integer (for an API Key Id), given \'%s\'', key_id.class.to_s))
  end
  raise ArgumentError.new('missing \'key_id\'') if( key_id.size.zero? )

  if(key_id.is_a?(String))
    data = api_keys.select { |_k,v| v['name'] == key_id }
    key_id = data.keys.first if( data )
  end

  return { 'status' => 404, 'message' => format( 'No API key \'%s\' found', key_id) } if( key_id.nil? )

  raise format('API Key can not be 0') if( key_id.zero? )

  endpoint = format('/api/auth/keys/%d', key_id)
  logger.debug("Deleting API key #{key_id} (DELETE #{endpoint})") if @debug

  delete(endpoint)

end