Class: Infisical::Secrets

Inherits:
Object
  • Object
show all
Defined in:
lib/infisical/secrets.rb

Overview

CRUD operations against Infisical's v4 secrets API.

Constant Summary collapse

BASE_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"api/v4/secrets"

Instance Method Summary collapse

Constructor Details

#initialize(http_client) ⇒ Secrets

Returns a new instance of Secrets.



14
15
16
# File 'lib/infisical/secrets.rb', line 14

def initialize(http_client)
  @http_client = http_client
end

Instance Method Details

#create(secret_name, secret_value, project_id:, environment:, secret_path: "/", secret_comment: nil, skip_multiline_encoding: nil) ⇒ Models::Secret

Creates a new secret.

Parameters:

  • secret_name (String)

    key of the secret to create

  • secret_value (String)

    value of the secret

  • project_id (String)

    id of the project to write to

  • environment (String)

    environment slug, e.g. "dev"

  • secret_path (String) (defaults to: "/")

    folder path to create the secret at

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

    optional comment stored with the secret

  • skip_multiline_encoding (Boolean, nil) (defaults to: nil)

    disable the API's encoding of multi-line values; omitted from the request when nil

Returns:

Raises:

  • (APIError)

    if the API rejects the request, e.g. the name is taken



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/infisical/secrets.rb', line 92

def create(secret_name, secret_value, project_id:, environment:, secret_path: "/", secret_comment: nil,
           skip_multiline_encoding: nil)
  response = @http_client.post(
    secret_path_for(secret_name),
    body: {
      projectId: project_id,
      environment: environment,
      secretPath: secret_path,
      secretValue: secret_value,
      secretComment: secret_comment,
      skipMultilineEncoding: skip_multiline_encoding
    }.compact
  )

  Models::Secret.from_api(response["secret"])
end

#delete(secret_name, project_id:, environment:, secret_path: "/") ⇒ Models::Secret

Deletes a secret.

Parameters:

  • secret_name (String)

    key of the secret to delete

  • project_id (String)

    id of the project to write to

  • environment (String)

    environment slug, e.g. "dev"

  • secret_path (String) (defaults to: "/")

    folder path the secret lives at

Returns:

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/infisical/secrets.rb', line 151

def delete(secret_name, project_id:, environment:, secret_path: "/")
  response = @http_client.delete(
    secret_path_for(secret_name),
    body: {
      projectId: project_id,
      environment: environment,
      secretPath: secret_path
    }
  )

  Models::Secret.from_api(response["secret"])
end

#get(secret_name, project_id:, environment:, secret_path: "/", include_imports: true) ⇒ Models::Secret

Fetches a single secret by name.

Parameters:

  • secret_name (String)

    key of the secret to fetch

  • project_id (String)

    id of the project to read from

  • environment (String)

    environment slug, e.g. "dev"

  • secret_path (String) (defaults to: "/")

    folder path the secret lives at

  • include_imports (Boolean) (defaults to: true)

    also look through imported folders when the secret is not found at the path itself

Returns:

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/infisical/secrets.rb', line 66

def get(secret_name, project_id:, environment:, secret_path: "/", include_imports: true)
  response = @http_client.get(
    secret_path_for(secret_name),
    params: {
      projectId: project_id,
      environment: environment,
      secretPath: secret_path,
      includeImports: include_imports
    }
  )

  Models::Secret.from_api(response["secret"])
end

#list(project_id:, environment:, secret_path: "/", include_imports: true, recursive: false, skip_unique_validation: false, attach_to_process_env: false) ⇒ Array<Models::Secret>

Lists the secrets in a project environment, sorted by key.

Parameters:

  • project_id (String)

    id of the project to read from

  • environment (String)

    environment slug, e.g. "dev"

  • secret_path (String) (defaults to: "/")

    folder path to list from

  • include_imports (Boolean) (defaults to: true)

    fold in secrets from imported folders; direct secrets win over imports on key conflicts, and earlier import blocks win over later ones

  • recursive (Boolean) (defaults to: false)

    also list secrets from sub-folders

  • skip_unique_validation (Boolean) (defaults to: false)

    in recursive mode, duplicate keys across folders are collapsed to one secret per key (last occurrence wins) unless this is true, in which case all of them are kept

  • attach_to_process_env (Boolean) (defaults to: false)

    export each fetched secret into the process environment (ENV), without overriding variables that are already set

Returns:

Raises:

  • (APIError)

    if the API rejects the request



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/infisical/secrets.rb', line 35

def list(project_id:, environment:, secret_path: "/", include_imports: true, recursive: false,
         skip_unique_validation: false, attach_to_process_env: false)
  response = @http_client.get(
    BASE_PATH,
    params: {
      projectId: project_id,
      environment: environment,
      secretPath: secret_path,
      includeImports: include_imports,
      recursive: recursive
    }
  )

  secrets = Array(response["secrets"]).map { |secret| Models::Secret.from_api(secret) }
  secrets = ensure_unique_secrets_by_key(secrets, skip_unique_validation) if recursive
  secrets = merge_imported_secrets(secrets, response["imports"]) if include_imports
  secrets = secrets.sort_by(&:secret_key)
  attach_to_env(secrets) if attach_to_process_env
  secrets
end

#update(secret_name, project_id:, environment:, secret_value: nil, new_secret_name: nil, secret_path: "/", skip_multiline_encoding: nil) ⇒ Models::Secret

Updates a secret's value, name, or both.

Parameters:

  • secret_name (String)

    key of the secret to update

  • project_id (String)

    id of the project to write to

  • environment (String)

    environment slug, e.g. "dev"

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

    new value, if changing it

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

    new key, if renaming

  • secret_path (String) (defaults to: "/")

    folder path the secret lives at

  • skip_multiline_encoding (Boolean, nil) (defaults to: nil)

    disable the API's encoding of multi-line values; omitted from the request when nil

Returns:

Raises:

  • (ArgumentError)

    if neither secret_value nor new_secret_name is given

  • (NotFoundError)

    if no such secret exists



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/infisical/secrets.rb', line 122

def update(secret_name, project_id:, environment:, secret_value: nil, new_secret_name: nil, secret_path: "/",
           skip_multiline_encoding: nil)
  if secret_value.nil? && new_secret_name.nil?
    raise ArgumentError, "update requires at least one of secret_value: or new_secret_name:"
  end

  response = @http_client.patch(
    secret_path_for(secret_name),
    body: {
      projectId: project_id,
      environment: environment,
      secretPath: secret_path,
      secretValue: secret_value,
      newSecretName: new_secret_name,
      skipMultilineEncoding: skip_multiline_encoding
    }.compact
  )

  Models::Secret.from_api(response["secret"])
end