diff --git a/.drone.yml b/.drone.yml index a4746a8..b5b1d6e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -9,8 +9,42 @@ steps: - name: build image: golang commands: + - go get -u github.com/gomarkdown/mdtohtml + - mdtohtml README.md index.htm - sh ./.drone.sh +# Copy binary to test server +- name: coadcorp-deploy + image: appleboy/drone-scp + settings: + host: + - 10.63.39.130 + username: l075239 + password: + from_secret: ssh_password + port: 22 + command_timeout: 2m + target: /home/l075239/smt/ + source: + - smt + - cbs_checksum.txt + - test.env + +# Start service +- name: coadcorp-restart + image: appleboy/drone-ssh + settings: + host: + - 10.63.39.130 + username: l075239 + password: + from_secret: ssh_password + port: 22 + command_timeout: 2m + script: + - sudo basc -c 'mv /home/l075239/smt/test.env /home/l075239/smt/.env' + - sudo bash -c '/etc/init.d/cbs restart' + - name: dell-deploy # # https://github.com/cschlosser/drone-ftps/blob/master/README.md image: cschlosser/drone-ftps diff --git a/README.md b/README.md index 53852fd..8497ae9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,17 @@ # Secrets Management Tool (SMT) +Build Date: {BUILDTIME} +Build Hash: {SHA1VER} + ## Overview -Provide REST API for CRUD to store and retrieve user/password data for logging into devices. Only password is encrypted, via AES256 GCM. Values stored in sqlite database. +Provide REST API for CRUD to store and retrieve secrets. Only password is encrypted, via AES256 GCM. Values stored encrypted within a sqlite database. -Requires JWT token to store/retrieve passwords. +A successful authentication returns a JWT token which must be provided for all other operations. -This isn't super secure, probably not even as secure as Hashicorp Vault running in dev mode. +Multiple user roles are supported, with each user only able to access secrets matching their user role. One exception is the built in administrator role that is able to access all secrets. + +Written by Nathan Coad (nathan.coad@dell.com) ## Installation @@ -18,7 +23,7 @@ This isn't super secure, probably not even as secure as Hashicorp Vault running ## Configuration |Environment Variable Name| Description | Example | Default | |--|--|--|--| -| LOG_FILE | Specify the name/path of file to write log messages to | /var/log/ccsecrets.log | ./ccsecrets.log +| LOG_FILE | Specify the name/path of file to write log messages to | /var/log/smt.log | ./smt.log | BIND_IP | Specify the local IP address to bind to. | 127.0.0.1 | Primary IPv4 address | | BIND_PORT | Specify the TCP/IP port to bind to. | 443 | 8443 | | TLS_KEY_FILE | Specify the filename of the TLS certificate private key (must be unencrypted) in PEM format | key.pem | privkey.pem | @@ -30,6 +35,8 @@ This isn't super secure, probably not even as secure as Hashicorp Vault running If the TLS certificate and key files cannot be located in the specified location, a self signed certificate will be generated with a 1 year validity period. +Example for generating API_SECRET and SECRETS_KEY is the following command on linux: `head /dev/urandom | tr -dc A-Za-z0-9 | head -c32` + ## Systemd script Create/update the systemd service definition at /etc/systemd/system/smt.service and then run systemctl daemon-reload diff --git a/index.htm b/index.htm new file mode 100644 index 0000000..59cbc2e --- /dev/null +++ b/index.htm @@ -0,0 +1,253 @@ +

Secrets Management Tool (SMT)

+ +

Build Date: {BUILDTIME} +Build Hash: {SHA1VER}

+ +

Overview

+ +

Provide REST API for CRUD to store and retrieve secrets. Only password is encrypted, via AES256 GCM. Values stored encrypted within a sqlite database.

+ +

A successful authentication returns a JWT token which must be provided for all other operations.

+ +

Multiple user roles are supported, with each user only able to access secrets matching their user role. One exception is the built in administrator role that is able to access all secrets.

+ +

Written by Nathan Coad (nathan.coad@dell.com)

+ +

Installation

+ +
    +
  1. Copy binary to chosen location, eg /srv/smt/smt
  2. +
  3. Create .env file in same directory as binary, populate as per Configuration section below
  4. +
  5. Create systemd service definition
  6. +
  7. Start service
  8. +
+ +

Configuration

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Environment Variable NameDescriptionExampleDefault
LOG_FILESpecify the name/path of file to write log messages to/var/log/smt.log./smt.log
BIND_IPSpecify the local IP address to bind to.127.0.0.1Primary IPv4 address
BIND_PORTSpecify the TCP/IP port to bind to.4438443
TLS_KEY_FILESpecify the filename of the TLS certificate private key (must be unencrypted) in PEM formatkey.pemprivkey.pem
TLS_CERT_FILESpecify the filename of the TLS certificate file in PEM formatcert.pemcert.pem
TOKEN_HOUR_LIFESPANNumber of hours that the JWT token returned at login is valid12No default specified, must define this value
API_SECRETSecret to use when generating JWT token3c55990bd479322e2053db3a8No default specified, must define this value
INITIAL_PASSWORDPassword to set for builtin Administrator account created when first started, can remove this value after first start. Can specify in plaintext or bcrypt hash$2a$10$s39a82wrRAdOJVZEkkrSReVnXprz5mxU30ZBO.dHPYTncQCsUD9cepassword
SECRETS_KEYKey to use for AES256 GCM encryption. Must be exactly 32 bytesAES256Key-32Characters1234567890No default specified, must define this value or use /api/unlock at runtime
+

If the TLS certificate and key files cannot be located in the specified location, a self signed certificate will be generated with a 1 year validity period.

+ +

Example for generating API_SECRET and SECRETS_KEY is the following command on linux: head /dev/urandom | tr -dc A-Za-z0-9 | head -c32

+ +

Systemd script

+ +

Create/update the systemd service definition at /etc/systemd/system/smt.service and then run systemctl daemon-reload

+ +
[Unit]
+Description=Secrets Management Tool
+After=network.target
+#StartLimitIntervalSec=0
+
+[Service]
+Type=simple
+Restart=always
+RestartSec=1
+User=root
+ExecStart=/srv/smt/smt
+
+[Install]
+WantedBy=multi-user.target
+
+ +

API

+ +

Unlock

+ +

POST /api/unlock

+ +

Data

+ +
{
+    "secretKey": "Example32ByteSecretKey0123456789"
+}
+
+ +

If the SECRETS_KEY environment variable is not defined, this API call to unlock stored secrets must be performed after initial startup of SMT. Storing/retrieval of secrets will not succeed until this API call has been made.

+ +

User Operations

+ +

Register

+ +

POST /api/admin/register

+ +

Data

+ +
{
+    "username": "", 
+    "password": "",
+    "RoleId": 2
+}
+
+ +

This operation can only be performed by a user with a role that is admin enabled. There are 3 built in roles, which can be viewed via the /api/admin/roles endpoint.

+ +

Login

+ +

POST /api/login

+ +

Data

+ +
{
+    "username": "", 
+    "password": ""
+}
+
+ +

This API call will return a JWT token that must be present for any other API calls to succeed. The validity duration of this token is based on the configured TOKEN_HOUR_LIFESPAN value. JWT token is returned as value of access_token.

+ +

List Roles

+ +

GET /api/admin/roles

+ +

This operation can only be performed by a user with a role that is admin enabled. Lists currently defined roles.

+ +

List Users

+ +

GET /api/admin/users

+ +

This operation can only be performed by a user with a role that is admin enabled. Lists currently defined users.

+ +

Secrets Operations

+ +

Store

+ +

POST /api/secret/store

+ +

Data

+ +
{
+    "deviceName": "",
+    "deviceCategory": "",
+    "userName": "",
+    "secretValue": ""
+}
+
+ +

Must be logged in to execute this command. Role of current user cannot be a ReadOnly role. Secret will be stored with the RoleId of the currently logged in user. Either deviceName or deviceCategory can be blank but not both.

+ +

If a secret exists for this RoleId and matching deviceName and deviceCategory then an error will be generated.

+ +

Retrieve

+ +

POST /api/secret/retrieve

+ +

Data

+ +
{
+    "deviceName": "",
+    "deviceCategory": "",
+    "userName": ""
+}
+
+ +

Must be logged in to execute this command. Only secrets registered with the current user’s RoleId can be retrieved.

+ +

Either deviceName or deviceCategory can be specified (or both). Wildcards are supported for both deviceName and deviceCategory fields. userName can also be specified in conjunction with deviceName or deviceCategory. +1. The percent sign % wildcard matches any sequence of zero or more characters. +2. The underscore _ wildcard matches any single character.

+ +

GET /api/secret/retrieve/name/<searchname>

+ +

Search for a secret specified by deviceName using a GET request. +Must be logged in to execute this command. Only secrets registered with the current user’s RoleId can be retrieved.

+ +

GET /api/secret/retrieve/category/<searchname>

+ +

Search for a secret specified by deviceCategory using a GET request. +Must be logged in to execute this command. Only secrets registered with the current user’s RoleId can be retrieved.

+ +

Update

+ +

POST /api/secret/update

+ +

Data

+ +
{
+    "deviceName": "",
+    "deviceCategory": "",
+    "userName": "",
+    "secretValue": ""
+}
+
+ +

Users with ReadOnly role will receive Forbidden error when calling this API endpoint. The values specified in deviceName and deviceCategory must match exactly one existing secret record for the RoleId of the currently logged in user. Wildcards are supported for deviceName and deviceCategory.

+ +

List

+ +

GET /api/secret/list

+ +

Will generate a list of device names and categories but not secret data.

diff --git a/main.go b/main.go index 630f977..a82673a 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "bytes" "context" "crypto/tls" + "embed" "fmt" "log" "net/http" @@ -23,10 +25,61 @@ import ( var sha1ver string // sha1 revision used to build the program var buildTime string // when the executable was built -var keyString string +type Replacements map[string]string + +var replacements = make(Replacements) + +//go:embed index.htm +var staticContent embed.FS + +// staticFileServer serves files from the provided fs.FS +func staticFileServer(content embed.FS) gin.HandlerFunc { + return func(c *gin.Context) { + // Get the requested file name + fileName := c.Request.URL.Path + + // Root request should load index.htm + if len(fileName) == 1 { + log.Printf("staticFileServer replacing root request with index.htm") + fileName = "index.htm" + } + + //log.Printf("staticFileServer attempting to load filename '%s'\n", fileName) + + // Try to open the file from the embedded FS + file, err := content.Open(fileName) + if err != nil { + c.String(http.StatusNotFound, "File not found") + return + } + defer file.Close() + + // Serve the file contents + fileStat, _ := file.Stat() + data := make([]byte, fileStat.Size()) + _, err = file.Read(data) + if err != nil { + c.String(http.StatusInternalServerError, "Error reading file") + return + } + + // parse the file and perform text replacements as necessary + for key, element := range replacements { + log.Printf("Searching for '%s' to replace\n", key) + data = bytes.Replace(data, []byte(key), []byte(element), -1) + } + + // Set the proper Content-Type header based on file extension + c.Header("Content-Type", http.DetectContentType(data)) + c.Data(http.StatusOK, "", data) + } +} func main() { + replacements["{SHA1VER}"] = sha1ver + replacements["{BUILDTIME}"] = buildTime + // Load data from environment file envFilename := utils.GetFilePath(".env") err := godotenv.Load(envFilename) @@ -53,7 +106,7 @@ func main() { models.ConnectDatabase() // Set secrets key from .env file - keyString = os.Getenv("SECRETS_KEY") + keyString := os.Getenv("SECRETS_KEY") if keyString != "" { // Key was defined in environment variable, let the models package know our secrets key @@ -83,10 +136,12 @@ func main() { // Recovery middleware recovers from any panics and writes a 500 if there was one. router.Use(gin.Recovery()) - // TODO - think of a better default landing page - router.GET("/", func(c *gin.Context) { - c.String(http.StatusOK, fmt.Sprintf("SMT Built on %s from sha1 %s\n", buildTime, sha1ver)) - }) + /* + // TODO - think of a better default landing page + router.GET("/", func(c *gin.Context) { + c.String(http.StatusOK, fmt.Sprintf("SMT Built on %s from sha1 %s\n", buildTime, sha1ver)) + }) + */ // Set some options for TLS tlsConfig := &tls.Config{ @@ -144,6 +199,13 @@ func main() { TLSConfig: tlsConfig, } + // Set the default readme page + //router.Use(EmbedReact("/", "static_files", staticDir)) + //router.Use(static.Serve("/", static.LocalFile("./static_files", true))) + + // Serve the embedded HTML file if no other routes match + router.NoRoute(staticFileServer(staticContent)) + // Register our routes public := router.Group("/api") public.POST("/login", controllers.Login) diff --git a/test.env b/test.env new file mode 100644 index 0000000..b45fed1 --- /dev/null +++ b/test.env @@ -0,0 +1,8 @@ +LOG_FILE=/var/log/smt.log +API_SECRET=ArCRbRoIyN2tTaG613v4skKHHZtXZ0Vy +INITIAL_PASSWORD=Password123 +TOKEN_HOUR_LIFESPAN=168 +BIND_IP= +BIND_PORT=8443 +TLS_KEY_FILE=key.pem +TLS_CERT_FILE=cert.pem \ No newline at end of file