Skip to content

Commit cfce105

Browse files
authored
Merge pull request #59 from Infisical/daniel/agent-manage-leases
feat(agent): persistent caching for agent
2 parents a760abc + b10c74e commit cfce105

15 files changed

Lines changed: 1004 additions & 92 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ agent-config.test.yaml
88
infisical-merge
99
test/infisical-merge
1010
.DS_Store
11+
12+
13+
/agent-testing

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ require (
7272
github.com/aws/aws-sdk-go-v2/service/sts v1.28.12 // indirect
7373
github.com/aws/smithy-go v1.20.2 // indirect
7474
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
75+
github.com/cespare/xxhash v1.1.0 // indirect
76+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
7577
github.com/chzyer/readline v1.5.1 // indirect
7678
github.com/danieljoos/wincred v1.2.0 // indirect
7779
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
80+
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
81+
github.com/dgraph-io/ristretto v0.1.1 // indirect
82+
github.com/dustin/go-humanize v1.0.0 // indirect
7883
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
7984
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
8085
github.com/felixge/httpsnoop v1.0.4 // indirect
@@ -91,8 +96,11 @@ require (
9196
github.com/godbus/dbus/v5 v5.1.0 // indirect
9297
github.com/gofrs/flock v0.8.1 // indirect
9398
github.com/gogo/protobuf v1.3.2 // indirect
99+
github.com/golang/glog v1.2.0 // indirect
94100
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
95101
github.com/golang/protobuf v1.5.4 // indirect
102+
github.com/golang/snappy v0.0.3 // indirect
103+
github.com/google/flatbuffers v1.12.1 // indirect
96104
github.com/google/gnostic-models v0.6.9 // indirect
97105
github.com/google/go-cmp v0.7.0 // indirect
98106
github.com/google/gofuzz v1.2.0 // indirect

go.sum

Lines changed: 45 additions & 0 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,57 @@ Copyright (c) 2023 Infisical Inc.
44
package main
55

66
import (
7+
"fmt"
78
"os"
9+
"strings"
10+
"time"
811

912
"github.com/Infisical/infisical-merge/packages/cmd"
1013
"github.com/rs/zerolog"
1114
"github.com/rs/zerolog/log"
1215
)
1316

1417
func main() {
15-
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
18+
19+
// very annoying but zerolog doesn't allow us to change one color without changing all of them
20+
// these are the default colors for each level, except for warn
21+
levelColors := map[string]string{
22+
"trace": "\033[35m", // magenta
23+
"debug": "\033[33m", // yellow
24+
"info": "\033[32m", // green
25+
"warn": "\033[33m", // yellow (this one is custom, the default is red \033[31m)
26+
"error": "\033[31m", // red
27+
"fatal": "\033[31m", // red
28+
"panic": "\033[31m", // red
29+
}
30+
31+
// map full level names to abbreviated forms (default zerolog behavior)
32+
// see consoleDefaultFormatLevel, in zerolog for example
33+
levelAbbrev := map[string]string{
34+
"trace": "TRC",
35+
"debug": "DBG",
36+
"info": "INF",
37+
"warn": "WRN",
38+
"error": "ERR",
39+
"fatal": "FTL",
40+
"panic": "PNC",
41+
}
42+
43+
log.Logger = log.Output(zerolog.ConsoleWriter{
44+
Out: os.Stderr,
45+
TimeFormat: time.RFC3339,
46+
FormatLevel: func(i interface{}) string {
47+
level := fmt.Sprintf("%s", i)
48+
color := levelColors[level]
49+
if color == "" {
50+
color = "\033[0m" // no color for unknown levels
51+
}
52+
abbrev := levelAbbrev[level]
53+
if abbrev == "" {
54+
abbrev = strings.ToUpper(level) // fallback to uppercase if unknown
55+
}
56+
return color + abbrev + "\033[0m"
57+
},
58+
})
1659
cmd.Execute()
1760
}

packages/api/api.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"encoding/base64"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strings"
@@ -55,6 +56,8 @@ const (
5556
operationCallInstanceRelayHeartBeat = "CallInstanceRelayHeartBeat"
5657
)
5758

59+
var ErrNotFound = errors.New("resource not found")
60+
5861
func CallGetEncryptedWorkspaceKey(httpClient *resty.Client, request GetEncryptedWorkspaceKeyRequest) (GetEncryptedWorkspaceKeyResponse, error) {
5962
endpoint := fmt.Sprintf("%v/v2/workspace/%v/encrypted-key", config.INFISICAL_URL, request.WorkspaceId)
6063
var result GetEncryptedWorkspaceKeyResponse
@@ -573,6 +576,31 @@ func CallCreateDynamicSecretLeaseV1(httpClient *resty.Client, request CreateDyna
573576
return createDynamicSecretLeaseResponse, nil
574577
}
575578

579+
func CallGetDynamicSecretLeaseV1(httpClient *resty.Client, request GetDynamicSecretLeaseV1Request) (GetDynamicSecretLeaseV1Response, error) {
580+
var getDynamicSecretLeaseResponse GetDynamicSecretLeaseV1Response
581+
response, err := httpClient.
582+
R().
583+
SetResult(&getDynamicSecretLeaseResponse).
584+
SetHeader("User-Agent", USER_AGENT).
585+
SetQueryParam("environmentSlug", request.Environment).
586+
SetQueryParam("projectSlug", request.ProjectSlug).
587+
SetQueryParam("secretPath", request.SecretPath).
588+
Get(fmt.Sprintf("%v/v1/dynamic-secrets/leases/%s", config.INFISICAL_URL, request.LeaseID))
589+
590+
if err != nil {
591+
return GetDynamicSecretLeaseV1Response{}, fmt.Errorf("CallGetDynamicSecretLeaseV1: Unable to complete api request [err=%w]", err)
592+
}
593+
594+
if response.IsError() {
595+
if response.StatusCode() == http.StatusNotFound {
596+
return GetDynamicSecretLeaseV1Response{}, ErrNotFound
597+
}
598+
return GetDynamicSecretLeaseV1Response{}, fmt.Errorf("CallGetDynamicSecretLeaseV1: Unsuccessful response [status-code=%v] [response=%v]", response.StatusCode(), response.String())
599+
}
600+
601+
return getDynamicSecretLeaseResponse, nil
602+
}
603+
576604
func CallCreateRawSecretsV3(httpClient *resty.Client, request CreateRawSecretV3Request) error {
577605
response, err := httpClient.
578606
R().

packages/api/model.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,21 @@ type CreateDynamicSecretLeaseV1Response struct {
585585
Data map[string]interface{} `json:"data"`
586586
}
587587

588+
type GetDynamicSecretLeaseV1Request struct {
589+
LeaseID string
590+
Environment string
591+
ProjectSlug string
592+
SecretPath string
593+
}
594+
595+
type GetDynamicSecretLeaseV1Response struct {
596+
Lease struct {
597+
Id string `json:"id"`
598+
ExpireAt time.Time `json:"expireAt"`
599+
} `json:"lease"`
600+
DynamicSecret models.DynamicSecret `json:"dynamicSecret"`
601+
}
602+
588603
type GetLoginV3Request struct {
589604
Email string `json:"email"`
590605
Password string `json:"password"`

0 commit comments

Comments
 (0)