Skip to content

Instantly share code, notes, and snippets.

@consideRatio
Last active April 7, 2023 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save consideRatio/1c42cc9f07a7545cb81ec98219629f15 to your computer and use it in GitHub Desktop.
Save consideRatio/1c42cc9f07a7545cb81ec98219629f15 to your computer and use it in GitHub Desktop.
Helm helper template randHex to generate random hexadecimal (hex, base16) numbers
{{/*
Returns given number of random Hex characters.
In practice, it generates up to 100 randAlphaNum strings
that are filtered from non-hex characters and augmented
to the resulting string that is finally trimmed down.
*/}}
{{- define "jupyterhub.randHex" -}}
{{- $result := "" }}
{{- range $i := until 100 }}
{{- if lt (len $result) . }}
{{- $rand_list := randAlphaNum . | splitList "" -}}
{{- $reduced_list := without $rand_list "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" }}
{{- $rand_string := join "" $reduced_list }}
{{- $result = print $result $rand_string -}}
{{- end }}
{{- end }}
{{- $result | trunc . }}
{{- end }}
@giuliocalzolari
Copy link

giuliocalzolari commented Nov 14, 2022

different approach using shuffle

{{/*
include "randHex" 2  > result "4e"
*/}}
{{- define "randHex" -}}
    {{- $result := "" -}}
    {{- range $i := until . -}}
        {{- $base := shuffle "0123456789abcdef" -}}
        {{- $i_curr := (randNumeric 1) | int -}}
        {{- $i_next := add $i_curr 1 | int -}}
        {{- $rand_hex := substr $i_curr $i_next $base -}}
        {{- $result = print $result $rand_hex -}}
    {{- end -}}
    {{- $result -}}
{{- end -}}

@consideRatio
Copy link
Author

Thanks for sharing @giuliocalzolari! This is what the open-source project I contribute to ended up doing:

{{- /*
    Returns given number of random Hex characters.
    - randNumeric 4 | atoi generates a random number in [0, 10^4)
      This is a range range evenly divisble by 16, but even if off by one,
      that last partial interval offsetting randomness is only 1 part in 625.
    - mod N 16 maps to the range 0-15
    - printf "%x" represents a single number 0-15 as a single hex character
*/}}
{{- define "jupyterhub.randHex" -}}
    {{- $result := "" }}
    {{- range $i := until . }}
        {{- $rand_hex_char := mod (randNumeric 4 | atoi) 16 | printf "%x" }}
        {{- $result = print $result $rand_hex_char }}
    {{- end }}
    {{- $result }}
{{- end }}

From https://github.com/jupyterhub/zero-to-jupyterhub-k8s/blob/2.0.0/jupyterhub/templates/hub/_helpers-passwords.tpl#L16-L32.

About persisting generated credentials between helm chart upgrade

If Helm templates are used to generate a key like this, one may run into issues when they are re-generated on the next helm upgrade. Ideally they would be re-used instead.

This is possible, and done in practice by the JupyterHub Helm chart, thanks to the lookup function. See for example a helper function to generate some credentials and where its used to set the value of a key in a k8s Secret managed by the Helm chart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment