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

Add util for random_string with special characters #1177

Open
saraburns1 opened this issue Dec 11, 2024 · 4 comments
Open

Add util for random_string with special characters #1177

saraburns1 opened this issue Dec 11, 2024 · 4 comments

Comments

@saraburns1
Copy link

def random_string(length: int) -> str:

Add new util random_string_special that includes string.punctuation(https://docs.python.org/3/library/string.html#string.punctuation)

This is needed when using random_string to create Clickhouse passwords

@regisb
Copy link
Contributor

regisb commented Dec 11, 2024

I'm guessing this is required in the context of Aspects, right? Have you considered using the ENV_TEMPLATE_VARIABLES filter?

@saraburns1
Copy link
Author

Yes for Aspects. I haven't considered that, but I'm not sure if it helps - I still need to generate random strings but with special characters included. This is what we're doing now ("ASPECTS_CLICKHOUSE_LRS_PASSWORD", "{{ 24|random_string }}"),

@DawoudSheraz DawoudSheraz moved this from Pending Triage to Backlog in Tutor project management Dec 12, 2024
@regisb
Copy link
Contributor

regisb commented Dec 12, 2024

You could implement what you need with the following:

def random_string_special(length):
    return "".join([random.choice(string.ascii_letters + string.digits + string.punctuation) for _ in range(lenth)])

hooks.Filters.ENV_TEMPLATE_VARIABLES.add_item(("random_string_special", random_string_special))

And then, in configuration:

("ASPECTS_CLICKHOUSE_LRS_PASSWORD", "{{ 24|random_string_special }}"),

I'm not saying we should not include "random_string_special" in Tutor core. I just want to highlight that if you need it now in Aspects, then you can add to in your plugin.

FYI you should be careful about inclusion of such strings in templates. For instance, quote signs will not be escaped, which means that you should always wrap those strings in double quotes "...". But string.punctuation also includes double quotes, so I'm not sure how to wrap those...

@saraburns1
Copy link
Author

Oh, perfect! I was going to just write a new util function but this is cleaner. Thank you!

vahid75 added a commit to vahid75/tutor that referenced this issue Dec 15, 2024
If you pass argument: special=True, special characters will be added to the generated random string.
Close overhangio#1177
vahid75 added a commit to vahid75/tutor that referenced this issue Dec 15, 2024
If you pass argument: special=True, special characters will be added to the generated random string.
Close overhangio#1177
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants