Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more automatic generator functions (#694) #866

Merged
merged 11 commits into from
Nov 10, 2022

Conversation

MatteoVoges
Copy link
Contributor

@MatteoVoges MatteoVoges commented Oct 13, 2022

Fixes issue #694

Proposed Changes

New generic random function:
Generates a random string based on given parameters.

Usage

secret: ?{ref:path/to/secret||random:type:nchars:special}

The first parameter type has to be one of:

  • str : generator function for alphanumeric characters, will be url-token-safe
  • int : generator function for digits (0-9)
  • loweralpha : generator function for lowercase letters (a-z)
  • upperalpha : generator function for uppercase letters (A-Z)
  • loweralphanum : generator function for lowercase letters and numbers (a-z and 0-9)
  • upperalphanum : generator function for uppercase letters and numbers (A-Z and 0-9)
  • special : generator function for alphanumeric characters and given special characters

I'm always open for name suggestions.

All these types have in common, that the second parameter nchars indicates how many characters the generated secret contains.
The default value for this is

  • random:str : 43 characters
  • random:int : 16 characters
  • other : 8 characters

The special type has a third argument that indicates which special characters are allowed for the generation.
The default for that is string.punctuation (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~).

NOTE: in manual usage the characters | and : are used for function and argument parsing, so they can't be specified.

This might be a

Breaking change

To pass special characters into the generator pool, we have to change the REF_TOKEN_TAG_PATTERN-regex.
The new regex (\?{(\w+:[\w\-\.\@\=\/\:]+)(\|(?:(?:\|\w+)(?::\S*)*)+)?\=*}) matches all existing use cases, but is more restricted.
So be beware of unexpected errors.

Furthermore the existing generator functions randomstr and lowernumalpha are still working and still behave the same.
But they will be removed soon.

Examples

Here are some example cases with expected output:

  • Tag: ?{ref:path/to/secret||random:int} generates a secret containing 16 (default) digits, i.e. 1254872907909324
  • Tag: ?{ref:path/to/secret||random:upperalphanum:12} generates a secret containing 12 chars from uppercase letters and numbers, i.e. VUPA0GIO47C2
  • Tag: ?{ref:path/to/secret||random:special:16:!$%&} generates a secret containing 16 chars from alphanumeric letters and given special characters !$%&, i.e. !kQT&m%csSDsdaBy

You could leave the argument empty, so ?{ref:path/to/secret||random:special:12:} generates a alphanumeric string without any special characters.

Documentation and testing

  • Updated Tests
  • TBD: Update docs for latest iteration.

Minor Issue

tl;dr: Some errors don't get caught and could produce some unexpected behavior.
See detailed: #694 (comment)

@ademariag
Copy link
Contributor

suggestion from Kapitan Workgroup:

introduce one new function random with the following parameters

random:<class>:<length>

where class is one of:

str: same as currently using randomstr
int : generator function for digits (0-9)
loweralpha : generator function for lowercase letters (a-z)
upperalpha : generator function for uppercase letters (A-Z)
upperalphanum : generator function for uppercase letters and numbers (A-Z and 0-9)
alphanumspec : generator function for alphanumeric characters and given special characters

i.e.
random:str produces same results as current randomstr
random:int produces a random int
random:loweralpha:15 produces a 15 character loweralpha string

@MatteoVoges
Copy link
Contributor Author

Do you think in test_refs.py every single option for type should be checked? Or is it enough to take some samples?

@ramaro ramaro merged commit 558f1f7 into kapicorp:master Nov 10, 2022
@MatteoVoges MatteoVoges deleted the automatic-generators branch November 11, 2022 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants