learning-it by example

#163 MLOps and deployment 6.8 min

Secrets in ML Apps: Scan, Move Out, Rotate (Python)

I planted 5 made-up secrets in a made-up ML app; a two-rule scanner found 4, missed 1 and raised 1 false alarm.

1. Why is deleting a leaked key from the code not enough?
2. The video planted five made-up secrets and scored its two-rule scanner. What was the result?
3. What does the video say about the two rules of its Python secret scanner?
4. In the fixed app, where does the secret() function look for a value, in order?
5. In what order does the video's rotate function work, and why?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Widen the key's scopes

    Copy the code folder first. In run.py in the rotate branch change keys.issue({"predict"}) to keys.issue({"predict", "delete_model"}). Predict what the second line will say, then run python run.py rotate. On my machine delete_model was allowed for key-1, which is why the video gives the app only the predict scope.

    A hint

    Compare with the video, where key-1 is refused delete_model.

  2. Remove the false alarm

    In scan.py change {8,} to {20,} in the named rule. Predict which line stops being flagged, then run python run.py scan. On my machine the false alarms fell to 0 while the scanner still found 4 and missed 1.

    A hint

    The tokenizer value is short compared with the keys.

  3. Rotate to a different scope

    In run.py in the rotate branch change the rotate call so the new key gets {"delete_model"} instead of {"predict"}. Predict what key-2 can do afterwards, then run python run.py rotate. On my machine the last line said key-2, predict: refused (not in scope).

    A hint

    Scopes are chosen when a key is issued.

  4. Catch the missed key

    The scanner misses the Bearer header. In scan.py add a third rule to RULES for the word Bearer followed by at least 20 letters or digits, then run python run.py scan. On my machine the result became found 5, missed 0, false alarms 1. Then decide whether such a rule would also catch real keys, remembering that the video's rules only fit its own made-up values.

    A hint

    Use a raw string such as r"Bearer\s+[A-Za-z0-9]{20,}".

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

A made-up app with made-up keys built at run time; a two-rule scanner tuned to those keys (no real scanner is run); a text file stands in for git history and a dictionary for the key service; no secret manager is used.

Up next in MLOps and deployment

More in MLOps and deployment

All MLOps and deployment videos

Related videos