Class: Pvectl::Config::Models::User

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/config/models/user.rb

Overview

Represents user credentials for Proxmox API authentication.

User is an immutable value object supporting two authentication methods:

  • API Token authentication (token_id + token_secret)

  • Username/Password authentication (username + password)

Examples:

Creating a user with token authentication

user = User.new(
  name: "admin",
  token_id: "root@pam!automation",
  token_secret: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
)
user.token_auth? #=> true

Creating a user with password authentication

user = User.new(
  name: "admin",
  username: "root@pam",
  password: "secret"
)
user.password_auth? #=> true

Constant Summary collapse

SECRET_MASK =

Mask used to hide secrets in output

"********"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, token_id: nil, token_secret: nil, username: nil, password: nil) ⇒ User

Creates a new User instance.

Parameters:

  • name (String)

    unique name for this user

  • token_id (String, nil) (defaults to: nil)

    API token ID

  • token_secret (String, nil) (defaults to: nil)

    API token secret

  • username (String, nil) (defaults to: nil)

    username for password auth

  • password (String, nil) (defaults to: nil)

    password for password auth



54
55
56
57
58
59
60
# File 'lib/pvectl/config/models/user.rb', line 54

def initialize(name:, token_id: nil, token_secret: nil, username: nil, password: nil)
  @name = name
  @token_id = token_id
  @token_secret = token_secret
  @username = username
  @password = password
end

Instance Attribute Details

#nameString (readonly)

Returns unique name identifying this user.

Returns:

  • (String)

    unique name identifying this user



33
34
35
# File 'lib/pvectl/config/models/user.rb', line 33

def name
  @name
end

#passwordString? (readonly)

Returns password for password auth.

Returns:

  • (String, nil)

    password for password auth



45
46
47
# File 'lib/pvectl/config/models/user.rb', line 45

def password
  @password
end

#token_idString? (readonly)

Returns API token ID (e.g., “root@pam!tokenid”).

Returns:

  • (String, nil)

    API token ID (e.g., “root@pam!tokenid”)



36
37
38
# File 'lib/pvectl/config/models/user.rb', line 36

def token_id
  @token_id
end

#token_secretString? (readonly)

Returns API token secret (UUID format).

Returns:

  • (String, nil)

    API token secret (UUID format)



39
40
41
# File 'lib/pvectl/config/models/user.rb', line 39

def token_secret
  @token_secret
end

#usernameString? (readonly)

Returns username for password auth (e.g., “root@pam”).

Returns:

  • (String, nil)

    username for password auth (e.g., “root@pam”)



42
43
44
# File 'lib/pvectl/config/models/user.rb', line 42

def username
  @username
end

Class Method Details

.from_hash(hash) ⇒ User

Creates a User from a kubeconfig-style hash structure.

Examples:

Hash structure for token auth

{
  "name" => "admin",
  "user" => {
    "token-id" => "root@pam!token",
    "token-secret" => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  }
}

Parameters:

  • hash (Hash)

    hash with “name” and “user” keys

Returns:

  • (User)

    new user instance



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pvectl/config/models/user.rb', line 96

def self.from_hash(hash)
  user_data = hash["user"] || {}

  new(
    name: hash["name"],
    token_id: user_data["token-id"],
    token_secret: user_data["token-secret"],
    username: user_data["username"],
    password: user_data["password"]
  )
end

Instance Method Details

#password_auth?Boolean

Checks if this user is configured for password authentication.

Returns:

  • (Boolean)

    true if both username and password are present



72
73
74
# File 'lib/pvectl/config/models/user.rb', line 72

def password_auth?
  !username.nil? && !username.empty? && !password.nil? && !password.empty?
end

#to_hash(mask_secrets: false) ⇒ Hash

Converts the user to a kubeconfig-style hash structure.

Parameters:

  • mask_secrets (Boolean) (defaults to: false)

    whether to mask sensitive values

Returns:

  • (Hash)

    hash representation suitable for YAML serialization



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pvectl/config/models/user.rb', line 112

def to_hash(mask_secrets: false)
  user_data = {}

  if token_auth?
    user_data["token-id"] = token_id
    user_data["token-secret"] = mask_secrets ? SECRET_MASK : token_secret
  end

  if password_auth?
    user_data["username"] = username
    user_data["password"] = mask_secrets ? SECRET_MASK : password
  end

  {
    "name" => name,
    "user" => user_data
  }
end

#token_auth?Boolean

Checks if this user is configured for API token authentication.

Returns:

  • (Boolean)

    true if both token_id and token_secret are present



65
66
67
# File 'lib/pvectl/config/models/user.rb', line 65

def token_auth?
  !token_id.nil? && !token_id.empty? && !token_secret.nil? && !token_secret.empty?
end

#valid?Boolean

Checks if this user has valid credentials for authentication.

Returns:

  • (Boolean)

    true if token auth or password auth is configured



79
80
81
# File 'lib/pvectl/config/models/user.rb', line 79

def valid?
  token_auth? || password_auth?
end