Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[knox] add no cache mode #103

Merged
merged 15 commits into from
Mar 1, 2024
88 changes: 88 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,94 @@ func (c *HTTPClient) getHTTPData(method string, path string, body url.Values, da
return nil
}


krockpot marked this conversation as resolved.
Show resolved Hide resolved
// UncachedHTTPClient is a client that uses HTTP to talk to Knox without caching.
type UncachedHTTPClient struct {
// CachedClient is the normal functional client
CachedClient HTTPClient
}

// NewClient creates a new uncached client to connect to talk to Knox.
func NewUncachedClient(host string, client HTTP, cachedClient HTTPClient, authHandler func() string, version string) APIClient {
return &UncachedHTTPClient{
CachedClient: cachedClient,
}
}

// NetworkGetKey gets a knox key by keyID and only uses network without the caches.
func (c *UncachedHTTPClient) NetworkGetKey(keyID string) (*Key, error) {
return c.CachedClient.NetworkGetKey(keyID)
}

// CacheGetKey acts same as NetworkGetKey for UncachedHTTPClient.
func (c *UncachedHTTPClient) CacheGetKey(keyID string) (*Key, error) {
return c.NetworkGetKey(keyID)
}

// GetKey gets a knox key by keyID.
func (c *UncachedHTTPClient) GetKey(keyID string) (*Key, error) {
return c.NetworkGetKey(keyID)
}

// CacheGetKeyWithStatus acts same as NetworkGetKeyWithStatus for UncachedHTTPClient.
func (c *UncachedHTTPClient) CacheGetKeyWithStatus(keyID string, status VersionStatus) (*Key, error) {
return c.NetworkGetKeyWithStatus(keyID, status)
}

// NetworkGetKeyWithStatus gets a knox key by keyID and given version status (always calls network).
func (c *UncachedHTTPClient) NetworkGetKeyWithStatus(keyID string, status VersionStatus) (*Key, error) {
return c.CachedClient.NetworkGetKeyWithStatus(keyID, status)
}

// GetKeyWithStatus gets a knox key by keyID and status (no cache).
func (c *UncachedHTTPClient) GetKeyWithStatus(keyID string, status VersionStatus) (*Key, error) {
return c.NetworkGetKeyWithStatus(keyID, status)
}

// CreateKey creates a knox key with given keyID data and ACL.
func (c *UncachedHTTPClient) CreateKey(keyID string, data []byte, acl ACL) (uint64, error) {
return c.CachedClient.CreateKey(keyID, data, acl)
}

// GetKeys gets all Knox (if empty map) or gets all keys in map that do not match key version hash.
func (c *UncachedHTTPClient) GetKeys(keys map[string]string) ([]string, error) {
return c.CachedClient.GetKeys(keys)
}

// DeleteKey deletes a key from Knox.
func (c UncachedHTTPClient) DeleteKey(keyID string) error {
return c.CachedClient.DeleteKey(keyID)
}

// GetACL gets a knox key by keyID.
func (c *UncachedHTTPClient) GetACL(keyID string) (*ACL, error) {
return c.CachedClient.GetACL(keyID)
}

// PutAccess will add an ACL rule to a specific key.
func (c *UncachedHTTPClient) PutAccess(keyID string, a ...Access) error {
return c.CachedClient.PutAccess(keyID, a)
}

// AddVersion adds a key version to a specific key.
func (c *UncachedHTTPClient) AddVersion(keyID string, data []byte) (uint64, error) {
return c.CachedClient.AddVersion(keyID, data)
}

// UpdateVersion either promotes or demotes a specific key version.
func (c *UncachedHTTPClient) UpdateVersion(keyID, versionID string, status VersionStatus) error {
return c.CachedClient.UpdateVersion(keyID, versionID, status)
}

func (c *UncachedHTTPClient) getClient() (HTTP, error) {
return c.CachedClient.getClient()
}

func (c *UncachedHTTPClient) getHTTPData(method string, path string, body url.Values, data interface{}) error {
return c.CachedClient.getHTTPData(method, path, body, data)
}


func getHTTPResp(cli HTTP, r *http.Request, resp *Response) error {
w, err := cli.Do(r)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion client/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ var clientGetKeyMetrics = func(map[string]string) {}
func Run(
client knox.APIClient,
yoyouwen marked this conversation as resolved.
Show resolved Hide resolved
p *VisibilityParams,
loginCommand *Command) {
loginCommand *Command,
) {

cli = client
if p != nil {
Expand Down
4 changes: 4 additions & 0 deletions client/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func parseTimeout(val string) (time.Duration, error) {
}

func runRegister(cmd *Command, args []string) *ErrorStatus {
if _, ok := cli.Type().FieldByName("KeyFolder"); !ok {
yoyouwen marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("Cannot Register in No Cache mode")
return nil
}
timeout, err := parseTimeout(*registerTimeout)
if err != nil {
return &ErrorStatus{fmt.Errorf("Invalid value for timeout flag: %s", err.Error()), false}
Expand Down