Configurable keylen, rename SNIP_FLASK_SECRET
All checks were successful
continuous-integration/drone/push Build is passing

- Length of snipped url is now configurable via `SNIP_KEYLEN` SNIP_FLASK_SECRET
- is renamed to SNIP_SECRET as it is also used internally in snip, not just
  flask
This commit is contained in:
Armin Friedl 2020-11-09 11:01:50 +01:00
parent 533c24df26
commit 26a79d15b4
4 changed files with 7 additions and 8 deletions

View file

@ -17,7 +17,7 @@ The simplest way to get started is to run snip from the regularily [published
docker containers](https://hub.docker.com/repository/docker/arminfriedl/snip). docker containers](https://hub.docker.com/repository/docker/arminfriedl/snip).
```shell ```shell
docker run -p5000 -e"SNIP_FLASK_SECRET=secretkey" arminfriedl/snip:latest docker run -p5000 -e"SNIP_SECRET=secretkey" arminfriedl/snip:latest
``` ```
Then navigate your browser to http://localhost:5000. Alternatively, you can also Then navigate your browser to http://localhost:5000. Alternatively, you can also

View file

@ -24,7 +24,7 @@ os.environ['FLASK_SKIP_DOTENV'] = str(snip_config.SNIP_FLASK_SKIP_DOTENV)
app = Flask(__name__) app = Flask(__name__)
app.config.update( app.config.update(
SECRET_KEY = snip_config.SNIP_FLASK_SECRET.get_secret_value(), SECRET_KEY = snip_config.SNIP_SECRET.get_secret_value(),
PREFERRED_URL_SCHEME = snip_config.SNIP_FLASK_PREFERRED_URL_SCHEME, PREFERRED_URL_SCHEME = snip_config.SNIP_FLASK_PREFERRED_URL_SCHEME,
SQLALCHEMY_DATABASE_URI = snip_config.SNIP_DATABASE_URI, SQLALCHEMY_DATABASE_URI = snip_config.SNIP_DATABASE_URI,
SQLALCHEMY_TRACK_MODIFICATIONS = snip_config.SNIP_DATABASE_TRACK_MODIFICATION) SQLALCHEMY_TRACK_MODIFICATIONS = snip_config.SNIP_DATABASE_TRACK_MODIFICATION)

View file

@ -39,7 +39,6 @@ class SnipConfig(BaseModel):
SNIP_DATABASE_TRACK_MODIFICATION: bool = False SNIP_DATABASE_TRACK_MODIFICATION: bool = False
# Flask settings # Flask settings
SNIP_FLASK_SECRET: SecretStr
SNIP_FLASK_ENVIRONMENT: Literal['development', 'production'] = 'production' SNIP_FLASK_ENVIRONMENT: Literal['development', 'production'] = 'production'
SNIP_FLASK_DEBUG: bool = False SNIP_FLASK_DEBUG: bool = False
SNIP_FLASK_SKIP_DOTENV: int = 1 SNIP_FLASK_SKIP_DOTENV: int = 1
@ -51,6 +50,8 @@ class SnipConfig(BaseModel):
# Snip settings # Snip settings
SNIP_STAGE: Optional[str] SNIP_STAGE: Optional[str]
SNIP_SECRET: SecretStr # also used as flask's SECRET_KEY
SNIP_KEYLEN: int = 5
def configure(stage: Optional[str] = None) -> SnipConfig: def configure(stage: Optional[str] = None) -> SnipConfig:
config_dict = {} config_dict = {}

View file

@ -1,3 +1,4 @@
from . import snip_config
from . import db from . import db
from .models import Snip from .models import Snip
@ -35,13 +36,10 @@ def snip(url: str, reusable=False) -> str:
def gen_snip(): def gen_snip():
""" Generate a random snip """ """ Generate a random snip """
rand = secrets.token_bytes(5) rand = secrets.token_bytes(snip_config.SNIP_KEYLEN)
snip = str(base58.b58encode(rand), 'ascii') snip = str(base58.b58encode(rand), 'ascii')
return snip return snip
def unsnip(snip: str): def unsnip(snip: str):
snip_dao = Snip.query.filter(Snip.snip == snip).first() return db.session.query(Snip.url).filter_by(snip=snip).scalar()
if snip_dao:
return snip_dao.url
return None