Skip to content

Commit

Permalink
Merge pull request #763 from tranchitella/omit-configuration-details
Browse files Browse the repository at this point in the history
feat: omit configuration values in the deployment details
  • Loading branch information
tranchitella committed Aug 18, 2022
2 parents 3da6c27 + f2a2c4a commit 294a9d1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
27 changes: 26 additions & 1 deletion model/configuration_deployment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,11 @@ import (
"github.com/pkg/errors"
)

const (
omitted = "...<omitted>"
lengthOmission = 3
)

// configuration saves the configuration as a binary blob
// It unmarshals any JSON type into a binary blob. Strings
// are escaped as it's decoded.
Expand Down Expand Up @@ -110,3 +115,23 @@ func NewDeploymentFromConfigurationDeploymentConstructor(

return deployment, nil
}

type deploymentConfiguration []byte

func (c deploymentConfiguration) MarshalJSON() ([]byte, error) {
var configuration map[string]string
err := json.Unmarshal(c, &configuration)
if err != nil {
return json.Marshal(c)
}
for key, value := range configuration {
if len(value) > lengthOmission {
configuration[key] = value[0:lengthOmission] + omitted
}
}
data, err := json.Marshal(configuration)
if err != nil {
return nil, err
}
return json.Marshal(data)
}
22 changes: 20 additions & 2 deletions model/configuration_deployment_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,32 @@
package model

import (
"encoding/base64"
"encoding/json"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

func TestConfigurationDeploymentValidate(t *testing.T) {
func TestDeploymentConfigurationMarshalJSON(t *testing.T) {
data := map[string]string{
"key": "value",
"another-key": "another-value",
}
dataJSON, err := json.Marshal(data)
assert.NoError(t, err)

var c deploymentConfiguration
c = deploymentConfiguration(dataJSON)

dataJSON, err = json.Marshal(c)
assert.NoError(t, err)
expected := base64.StdEncoding.EncodeToString([]byte("{\"another-key\":\"ano...\\u003comitted\\u003e\",\"key\":\"val...\\u003comitted\\u003e\"}"))
assert.Equal(t, "\""+expected+"\"", string(dataJSON))
}

func TestConfigurationDeploymentValidate(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
Expand Down
2 changes: 1 addition & 1 deletion model/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type Deployment struct {
// artifact for the device.
// The artifact will be generated when the device will ask
// for an update.
Configuration []byte `json:"configuration,omitempty" bson:"configuration"`
Configuration deploymentConfiguration `json:"configuration,omitempty" bson:"configuration"`
}

// NewDeployment creates new deployment object, sets create data by default.
Expand Down

0 comments on commit 294a9d1

Please sign in to comment.