arctic-sea icon indicating copy to clipboard operation
arctic-sea copied to clipboard

Add random setting

Open autermann opened this issue 7 years ago • 4 comments

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 https://github.com/52North/iceland/issues/32

autermann avatar Jan 08 '18 10:01 autermann

How about using UUID?

vigneshtdev avatar May 14 '19 03:05 vigneshtdev

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);
    }
}

autermann avatar May 14 '19 11:05 autermann

Is this issue still open?

arjunlal99 avatar Dec 27 '19 09:12 arjunlal99

Yes, the issue is still open.

CarstenHollmann avatar Jan 07 '20 13:01 CarstenHollmann