Code Monkey home page Code Monkey logo

secret-keeper's Introduction

license codecov build

clojars

Secret Keeper

A Clojure(Script) library for keeping your secrets under control.

Motivation

I want to be calm about sensitive data. This is security and responsibility to my clients. Specifying categories of sensitive data at the model level gives an understanding of what data we are working with.

Easy to mark up categories of sensitive data at model level and use them when reading configuration files, environment variables and also in the middlewares of the public API.

Installation

Add the following dependency in your project:

;; project.clj or build.boot
[team.sultanov/secret-keeper "1.0.86"]

;; deps.edn
team.sultanov/secret-keeper {:mvn/version "1.0.86"}

Usage

(ns example
  (:refer-clojure :exclude [read-string])
  (:require
    #?(:clj  [clojure.edn :refer [read-string]]
       :cljs [cljs.reader :refer [read-string]])
    [malli.core :as m]
    [secret.keeper :as keeper]
    [secret.keeper.malli :as keeper.malli]))


;;
;; Build secrets
;;

(def secret
  (keeper/make-secret {:passport "12345678"})) ; default category -> :secret


(keeper/secret? secret) ; => true

(prn secret) ; => #secret {:data "*** CENSORED ***", :category :secret}
(pr-str secret) ; => "#secret {:data \"*** CENSORED ***\", :category :secret}"

(keeper/data secret) ; => {:passport "12345678"}
(keeper/category secret) ; => :secret


;; Change the secret category

(-> secret
    (keeper/make-secret :personal)
    (keeper/category)) ; => :personal

;; nil and objects aren't a secret
(keeper/secret? "NOT A SECRET") ; => false
(keeper/data "NOT A SECRET") ; => "NOT A SECRET"
(keeper/category "NOT A SECRET") ; => nil

(keeper/secret? nil) ; => false
(keeper/make-secret nil) ; => nil
(keeper/data (keeper/make-secret nil)) ; => nil
(keeper/category (keeper/make-secret nil)) ; => nil



;;
;; Parse secrets
;;

(def read-secret
  (partial read-string {:readers {'secret keeper/make-secret}}))


;; Getting a secret from environment variables by symbols
;; For example, we have an environment variable: `$TEST_TOKEN=token_12345`

(def secret-token
  (read-secret "#secret TEST_TOKEN"))


(prn secret-token) ; => #secret {:data "*** CENSORED ***", :category :secret}
(pr-str secret-token) ; => "#secret {:data \"*** CENSORED ***\", :category :secret}"

(keeper/data secret-token) ; => "token_12345"
(keeper/category secret-token) ; => :secret


;; Getting a secret from environment variables by symbols with the custom category

(def secret-token+custom-category
  (read-secret "#secret {:data TEST_TOKEN, :category :confidential}"))


(prn secret-token+custom-category) ; => #secret {:data "*** CENSORED ***", :category :confidential}
(pr-str secret-token+custom-category) ; => "#secret {:data \"*** CENSORED ***\", :category :confidential}"

(keeper/data secret-token+custom-category) ; => "token_12345"
(keeper/category secret-token+custom-category) ; => :confidential



;;
;; Malli
;;

;; Transformer without any options

(= {:password "p4$$w0rd"}
   (m/decode [:map [:password string?]]
             {:password (keeper/make-secret "p4$$w0rd")}
             (keeper.malli/transformer))) ; => true


;; Transformer with some options:
;; - :key     - schema property key (by default ::keeper/category)
;; - :secrets - schema type or map key name

(def Transformer
  (keeper.malli/transformer
    {:key     :category
     :secrets {:passport :confidential
               :password :internal-only}}))


(def User
  [:map
   [:firstname string?]
   [:lastname string?]
   [:email string?]
   [:passport string?]
   [:address [:map {:category :personal} ; local category
              [:street string?]
              [:zip int?]
              [:city string?]
              [:country [:enum "USA"]]]]
   [:credentials [:map
                  [:login string?]
                  [:password string?]]]])


(def FakeUser
  {:firstname   "john"
   :lastname    "doe"
   :email       "[email protected]"
   :passport    "123456789"
   :address     {:street  "1488 Secret Street"
                 :zip     12345
                 :city    "Durham"
                 :country "USA"}
   :credentials {:login    "john"
                 :password "p4$$w0rd"}})


(m/encode User FakeUser Transformer)
;; =>
;; {:firstname   "john"
;;  :lastname    "doe"
;;  :email       "[email protected]"
;;  :passport    #secret{:data     "*** CENSORED ***"
;;                       :category :confidential}
;;  :address     #secret{:data     "*** CENSORED ***"
;;                       :category :personal}
;;  :credentials {:login    "john"
;;                :password #secret{:data     "*** CENSORED ***"
;;                                  :category :internal-only}}}

(= FakeUser
   (as-> FakeUser $
         (m/encode User $ Transformer)
         (m/decode User $ Transformer))) ; => true

Special thanks

License

Copyright © 2021 sultanov.team

secret-keeper's People

Contributors

just-sultanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

secret-keeper's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.