XOS>> eev>> 返回
项目作者: Apurer

项目描述 :
Go package for AES encryption and decryption of environment variable using private key.
高级语言: Go
项目地址: git://github.com/Apurer/eev.git
创建时间: 2020-05-25T00:11:45Z
项目社区:https://github.com/Apurer/eev

开源协议:MIT License

下载


Encrypted Environment Variable

GitHub Actions status

What is this?

This is Golang package with functions allowing for quick implementation of using encrypted environment variables within your code.
Usually you would have environment variables in plain text form to quickly read and use within your programs but it can be a problem to store it in this form sometimes.
If you would store your API key in bash_profile to make value persistant it is easy to unintentionally leak it.
Encrypting your environment variables is more secure way of obfuscating important values.

Helpful code snippets

Generate private key

  1. package main
  2. import (
  3. "github.com/Apurer/eev/privatekey"
  4. "flag"
  5. )
  6. func main() {
  7. keytype := flag.String("type", "ECDSA", "path to save private key")
  8. keysize := flag.Int("size", 192, "size of pivate key in bits")
  9. keypath := flag.String("path", "./private-key", "path to save private key")
  10. alg := flag.String("alg", "AES192", "encryption algorithm by which private key is encrypted")
  11. passphrase := flag.String("passphrase", "", "passphrase by which private key is encrypted")
  12. flag.Parse()
  13. switch *keytype {
  14. case "RSA":
  15. *keytype = "RSA PRIVATE KEY"
  16. case "ECDSA":
  17. *keytype = "ECDSA PRIVATE KEY"
  18. }
  19. privkey, err := privatekey.Generate(*keytype, *keysize)
  20. if err != nil {
  21. panic(err)
  22. }
  23. encryptionAlg := privatekey.AES192
  24. switch *alg {
  25. case "AES128":
  26. encryptionAlg = privatekey.AES128
  27. case "AES192":
  28. encryptionAlg = privatekey.AES192
  29. case "AES256":
  30. encryptionAlg = privatekey.AES256
  31. }
  32. err = privatekey.Write(*keypath, privkey, *passphrase, encryptionAlg)
  33. if err != nil {
  34. panic(err)
  35. }
  36. }

Get encrypted value on output using specific private key

  1. package main
  2. import (
  3. "github.com/Apurer/eev/privatekey"
  4. AES "github.com/Apurer/eev/aes"
  5. "flag"
  6. "fmt"
  7. )
  8. func main() {
  9. value := flag.String("value", "", "value to be encrypted")
  10. keypath := flag.String("key", "", "path to private key which is to be used for encryption of value")
  11. passphrase := flag.String("passphrase", "", "passphrase by which private key is encrypted")
  12. flag.Parse()
  13. privkey, err := privatekey.Read(*keypath, *passphrase)
  14. if err != nil {
  15. panic(err)
  16. }
  17. encrypted, err := AES.Encrypt(privkey, *value)
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Println(encrypted)
  22. }

Decrypt value using specific private key

  1. package main
  2. import (
  3. "github.com/Apurer/eev/privatekey"
  4. AES "github.com/Apurer/eev/aes"
  5. "flag"
  6. "fmt"
  7. )
  8. func main() {
  9. value := flag.String("value", "", "value to be decrypted")
  10. keypath := flag.String("key", "", "path to private key which is to be used for decryption of value")
  11. passphrase := flag.String("passphrase", "", "passphrase by which private key is decrypted")
  12. flag.Parse()
  13. privkey, err := privatekey.Read(*keypath, *passphrase)
  14. if err != nil {
  15. panic(err)
  16. }
  17. decrypted, err := AES.Decrypt(privkey, *value)
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Println(string(decrypted))
  22. }

Possible implementation in your program

  1. package main
  2. import (
  3. "github.com/Apurer/eev/privatekey"
  4. "github.com/Apurer/eev"
  5. "flag"
  6. )
  7. func main() {
  8. key := flag.String("key", "", "path to private key which is to be used for dencryption of environment variable")
  9. passphrase := flag.String("passphrase", "", "passphrase by which private key is encrypted")
  10. flag.Parse()
  11. privkey, err = privatekey.Read(*key, *passphrase)
  12. if err != nil {
  13. panic(err)
  14. }
  15. API_KEY, err := eev.Get("API_KEY", privkey)
  16. if err != nil {
  17. panic(err)
  18. }
  19. url := "http://example.com"
  20. fmt.Println("URL: ", url)
  21. req, err := http.NewRequest("GET", url, nil)
  22. req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", API_KEY))
  23. client := &http.Client{}
  24. resp, err := client.Do(req)
  25. if err != nil {
  26. panic(err)
  27. }
  28. defer resp.Body.Close()
  29. fmt.Println("response Status:", resp.Status)
  30. fmt.Println("response Headers:", resp.Header)
  31. body, _ := ioutil.ReadAll(resp.Body)
  32. fmt.Println("response Body:", string(body))
  33. }