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 random setting #10

Open
autermann opened this issue Jan 8, 2018 · 4 comments
Open

Add random setting #10

autermann opened this issue Jan 8, 2018 · 4 comments

Comments

@autermann
Copy link
Member

Add a new setting type that generates a random string (or number?) at every time the service starts. Can be used for security tokens etc.

See 52North/iceland#32

@vigneshtdev
Copy link

How about using UUID?

@autermann
Copy link
Member Author

This is more like an extension of a string setting but without a configurable default value. Instead the default value is generated every time the definition is created. You could test something like this:

public class RandomSettingDefinition 
        extends StringSettingDefinition implements Constructable {

    private static final String DEFAULT_ALLOWED_CHARACTERS =
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ"
    private boolean secure = true;
    private int length = 20;
    private int[] allowedCharacters = DEFAULT_ALLOWED_CHARACTERS.codePoints().toArray();

    public void setSecure(boolean secure) {
        this.secure = secure;
    }

    public boolean isSecure() {
        return this.secure;
    }

    public void setAllowedCharacters(String allowedCharacters) {
        Preconditions.checkArgument(allowedCharacters.length() > 0);
        this.allowedCharacters = allowedCharacters.codePoints().distinct().toArray();
    }

    public String getAllowedCharacters() {
        return Arrays.stream(this.allowedCharacters)
                .collect(StringBuilder::new,
                         StringBuilder::appendCodePoint,
                         StringBuilder::append)
                .toString();
    }

    public void setLength(int length) {
        Preconditions.checkArgument(length > 0);
        this.length = length;
    }

    public int getLength() {
        return this.length;
    }

    @Override
    public void init() {
        String value = (this.secure ? new SecureRandom() : new Random())
                .ints(this.length, 0, this.allowedCharacters.length)
                .map(idx -> this.allowedCharacters[idx])
                .collect(StringBuilder::new,
                         StringBuilder::appendCodePoint,
                         StringBuilder::append)
                .toString();
        setDefaultValue(value);
    }
}

@arjunlal99
Copy link

Is this issue still open?

@CarstenHollmann
Copy link

Yes, the issue is still open.

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

No branches or pull requests

4 participants