Skip to content

Instantly share code, notes, and snippets.

@devorbitus
Last active March 12, 2025 14:26
Show Gist options
  • Select an option

  • Save devorbitus/bb64300ecf1f88179300f55fedafcd5f to your computer and use it in GitHub Desktop.

Select an option

Save devorbitus/bb64300ecf1f88179300f55fedafcd5f to your computer and use it in GitHub Desktop.
Akeyless Nu Shell Account Reference Archive

Akeyless Nu Shell Account Reference Archive

This script will use an Akeyless T-Token to authenticate to the API and download account details for reference WITHOUT accessing secret values.

The script will create a datadirectory and place the JSON files into that directory.

Prerequisites

  • Install Nu Shell
  • Download the script and make it executable
  • Execute the script

Installation

Install Nu Shell onto the target system, then run this command within a nu shell.

This command will download the script and store it at the archive.nu locally and then make it executable.

let archive = (http get https://gist.githubusercontent.com/devorbitus/bb64300ecf1f88179300f55fedafcd5f/raw/818bdf143757eeeb66858e8250fa8efce935f0e9/akeyless-reference-archive.nu);
$archive | save --force archive.nu; chmod +x archive.nu

You can then run the command like so

./archive.nu
#!/usr/bin/env nu
# Define API endpoint
let api_endpoint = "api.akeyless.io"
# Define minimum token validity duration
let min_token_validity = 10min
def validate_akeyless_token [token: string] {
let validateToken = (http post --content-type application/json $"https://($api_endpoint)/validate-token" {token: $token})
let is_valid = ($validateToken | get is_valid)
if $is_valid {
let expirationDateString = ($validateToken | get expiration)
let now = (date now)
let timeUntilExpiration = ($expirationDateString | into datetime) - $now
if $timeUntilExpiration < $min_token_validity {
return {
is_valid: false,
message: $"Token will expire too soon (in ($timeUntilExpiration)). It must be valid for at least ($min_token_validity).",
expiration: $expirationDateString,
time_until_expiration: $timeUntilExpiration
}
} else {
return {
is_valid: true,
message: $"Token is valid and will expire in ($timeUntilExpiration).",
expiration: $expirationDateString,
time_until_expiration: $timeUntilExpiration
}
}
} else {
return {
is_valid: false,
message: "Token is invalid.",
expiration: null,
time_until_expiration: null
}
}
}
def get_akeyless_token [] {
mut input_token = ""
while true {
print $"Please enter your Akeyless token:"
let token_input = (input)
if ($token_input | is-empty) {
print $"(ansi red_bold)Error:(ansi reset) Token cannot be empty"
continue
}
if not ($token_input | str starts-with "t-") {
print $"(ansi red_bold)Error:(ansi reset) Token must start with 't-'"
continue
}
# Validate the token
let validation_result = (validate_akeyless_token $token_input)
if not ($validation_result.is_valid) {
print $"(ansi red_bold)Error:(ansi reset) ($validation_result.message)"
print "Please try again with a different token."
continue
}
print $"(ansi green_bold)Success:(ansi reset) ($validation_result.message)"
$input_token = $token_input
break
}
return $input_token
}
def check_akeyless_token [] {
if "AKEYLESS_TOKEN" in $env {
print $"(ansi green_bold)Success:(ansi reset) AKEYLESS_TOKEN environment variable is set"
# Validate the existing token
let validation_result = (validate_akeyless_token $env.AKEYLESS_TOKEN)
if not ($validation_result.is_valid) {
print $"(ansi red_bold)Error:(ansi reset) ($validation_result.message)"
print $"(ansi yellow)Getting a new token...(ansi reset)"
let input_token = (get_akeyless_token)
$env.AKEYLESS_TOKEN = $input_token
} else {
print $"(ansi green_bold)Success:(ansi reset) ($validation_result.message)"
}
} else {
print $"(ansi yellow)AKEYLESS_TOKEN not found in environment(ansi reset)"
let input_token = (get_akeyless_token)
$env.AKEYLESS_TOKEN = $input_token
}
}
# Run the check
check_akeyless_token
# Get token from environment variable
let token = $env.AKEYLESS_TOKEN
# Create data directory if it doesn't exist
let data_dir = (pwd | path join "data")
if ($data_dir | path exists) == false {
mkdir $data_dir
}
# List auth methods
let authMethods = (http post --content-type application/json $"https://($api_endpoint)/list-auth-methods" {token: $token} | get auth_methods)
# Describe each auth method and collect results
let authMethods = ($authMethods | each { |method|
let name = ($method.auth_method_name | into string)
print $"Processing auth method: ($name)"
let authMethod = (http post --content-type application/json $"https://($api_endpoint)/get-auth-method" {token: $token, name: $name})
$authMethod
})
# Save to file
$authMethods | save -f (pwd | path join "data" "akeyless-auth-methods-archive.json")
# List roles
let roles = (http post --content-type application/json $"https://($api_endpoint)/list-roles" {token: $token} | get roles)
# Describe each role and collect results
let roles = ($roles | each { |role|
let name = ($role.role_name | into string)
print $"Processing role: ($name)"
let role = (http post --content-type application/json $"https://($api_endpoint)/get-role" {token: $token, name: $name})
$role
})
# Save to file
$roles | save -f (pwd | path join "data" "akeyless-roles-archive.json")
# List Targets
let targets = (http post --content-type application/json $"https://($api_endpoint)/list-targets" {token: $token} | get targets)
# Describe each target and collect results
let targets = ($targets | each { |target|
let name = ($target.target_name | into string)
print $"Processing target: ($name)"
let target = (http post --content-type application/json $"https://($api_endpoint)/get-target" {token: $token, name: $name})
$target
})
# Save to file
$targets | save -f (pwd | path join "data" "akeyless-targets-archive.json")
# List items
let items = (http post --content-type application/json $"https://($api_endpoint)/list-items" {token: $token} | get items)
# Describe each item and collect results
let describedItems = ($items | par-each { |item|
let name = ($item.item_name | into string)
print $"Processing item: ($name)" # Debug line
let describeItem = (http post --content-type application/json $"https://($api_endpoint)/describe-item" {token: $token, name: $name})
$describeItem
})
# Save to file
$describedItems | save -f (pwd | path join "data" "akeyless-items-archive.json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment