Ravindra BagaleCourses & study guides मराठी

Chapter 94: HashiCorp Vault and Secrets Management

DevOps

94.17 Entirely local practice — a model, not a Vault server

Story (fictional teaching scenario). Rutuja from Satara and Ken from Kyoto use fake fields and a fake clock to learn policy and version ideas without running a real secrets service.

Entirely local practice — a model, not a Vault server
Entirely local practice — a model, not a Vault server

Why

A deterministic model makes the rules visible without installing a service or handling authentication material.

What

  • Teaching model simplifies behaviour; it is not a replacement for Vault.
  • Fake clock lets tests move time without sleeping.
  • Unit test checks the model’s Python behaviour, not real Vault security, API compatibility, cryptography or provider revocation.

How

  1. Create an empty local practice folder.

  2. Save the complete model.py and test_model.py below.

  3. Run python3 -m unittest -v.

  4. Use only DEMO fields; never enter credentials.

  5. Read the limitations before applying any idea to a real deployment.

Example 1: Complete model.py

"""Teaching model only. NOT Vault, NOT encryption, NOT a credential store.
Exact-path policy + fake clock + version/CAS mechanics for deterministic tests.
Never put real values into this program.
"""
from copy import deepcopy

class TeachingModel:
    def __init__(self):
        self.now = 0
        self.versions = {}
        self.deleted = set()
        self.revoked = False
        self.expires_at = 60
        self.rules = {'secret/data/shop': {'read'},
                      'secret/metadata/shop': {'read'}}

    def allowed(self, path, capability):
        if self.revoked or self.now >= self.expires_at:
            return False
        caps = self.rules.get(path, set())
        return 'deny' not in caps and capability in caps

    def write_demo(self, fields, cas):
        # Bypasses policies deliberately: simulates a teacher preparing data.
        current = len(self.versions)
        if type(cas) is not int or cas != current:
            raise ValueError('CAS mismatch')
        version = current + 1
        self.versions[version] = deepcopy(fields)
        return version

    def read(self, version=None):
        if not self.allowed('secret/data/shop', 'read'):
            raise PermissionError('denied')
        v = len(self.versions) if version is None else version
        if v in self.deleted:
            raise KeyError('soft deleted')
        return deepcopy(self.versions[v])

    def renew(self, seconds):
        if self.revoked or self.now >= self.expires_at:
            raise PermissionError('expired or revoked')
        if type(seconds) is not int or seconds <= 0:
            raise ValueError('positive seconds required')
        self.expires_at = min(self.now + seconds, 120)
        return self.expires_at - self.now
  1. The clock starts at zero and the model identity expires at sixty.
  2. Rules match exact paths only. No wildcard, policy-priority, identity-policy or namespace implementation exists here.
  3. write_demo deliberately bypasses policy to simulate teacher setup; it is not an authenticated API.
  4. CAS must equal the current number of versions. A stale value raises an error.
  5. Reads return copies so changing a returned dictionary does not edit stored demo data.
  6. deleted models soft-deletion visibility only. No cryptography, durable storage, network, audit or real token exists.
  7. Renewal is bounded by a made-up 120-unit model maximum. It is not a claim about Vault defaults.

Example 2: Complete test_model.py

import unittest
from model import TeachingModel

class ModelTests(unittest.TestCase):
    def setUp(self):
        self.m = TeachingModel()
        self.m.write_demo({'label': 'DEMO_ONLY'}, cas=0)
    def test_read(self):
        self.assertEqual(self.m.read()['label'], 'DEMO_ONLY')
    def test_default_deny(self):
        self.assertFalse(self.m.allowed('secret/data/other', 'read'))
    def test_no_update(self):
        self.assertFalse(self.m.allowed('secret/data/shop', 'update'))
    def test_explicit_deny(self):
        self.m.rules['secret/data/shop'].add('deny')
        self.assertFalse(self.m.allowed('secret/data/shop', 'read'))
    def test_cas(self):
        with self.assertRaises(ValueError):
            self.m.write_demo({'label': 'DEMO_V2'}, cas=0)
    def test_version(self):
        self.assertEqual(self.m.write_demo({'label': 'DEMO_V2'},cas=1),2)
        self.assertEqual(self.m.read(1)['label'],'DEMO_ONLY')
    def test_soft_delete(self):
        self.m.deleted.add(1)
        with self.assertRaises(KeyError): self.m.read(1)
        self.m.deleted.remove(1)
        self.assertEqual(self.m.read(1)['label'],'DEMO_ONLY')
    def test_expiry(self):
        self.m.now = 60
        self.assertFalse(self.m.allowed('secret/data/shop','read'))
    def test_renew_from_now(self):
        self.m.now = 30
        self.assertEqual(self.m.renew(60),60)
        self.assertEqual(self.m.expires_at,90)
    def test_max_lifetime(self):
        self.m.now = 50
        self.assertEqual(self.m.renew(100),70)
    def test_revoke(self):
        self.m.revoked = True
        with self.assertRaises(PermissionError): self.m.read()
    def test_copy_isolation(self):
        value=self.m.read();value['label']='CHANGED'
        self.assertEqual(self.m.read()['label'],'DEMO_ONLY')

if __name__ == '__main__': unittest.main()

Example 3: Run deterministic tests

  1. Read the command or request.
python3 -m unittest -v
  1. Read the result.

Output (tested local teaching model; NOT Vault)

Ran 12 tests
OK
  1. Line by line: Tests check exact-path default deny, no update grant, explicit deny, CAS/version behaviour, soft-delete visibility, expiry, renewal from now, a model max lifetime, revoke and copy isolation. All twelve passed locally. No actual Vault CLI/API, lease provider or encryption was tested.

Ravindra Bagale's Tip

The model is for learning, never for storing secrets.

Did you understand? Let us try it once more.