Skip to content

Commit

Permalink
Backwards compatibility for smbios headers (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Aug 12, 2022
1 parent be788bc commit 72971ff
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pkg/server/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,21 @@ func buildName(data map[string]interface{}, name string) string {

func getSMBios(req *http.Request) (map[string]interface{}, error) {
var smbios string
// 200 * 875bytes per header = 175Kb of smbios data, should be enough?
for i := 1; i <= 200; i++ {
header := req.Header.Get(fmt.Sprintf("X-Cattle-Smbios-%d", i))
if header == "" {
break
// Old header sent by clients on commit < be788bcfd899977770d84c996abd967c30942822
headerOld := req.Header.Get("X-Cattle-Smbios")

// If old header not found try to get the new ones
if headerOld == "" {
// 200 * 875bytes per header = 175Kb of smbios data, should be enough?
for i := 1; i <= 200; i++ {
header := req.Header.Get(fmt.Sprintf("X-Cattle-Smbios-%d", i))
if header == "" {
break
}
smbios = smbios + header
}
smbios = smbios + header
} else {
smbios = headerOld
}

if smbios == "" {
Expand Down
60 changes: 60 additions & 0 deletions pkg/server/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ limitations under the License.
package server

import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"testing"

values "github.com/rancher/wrangler/pkg/data"
"gotest.tools/assert"
)

Expand Down Expand Up @@ -95,3 +100,58 @@ func TestBuildName(t *testing.T) {
assert.Equal(t, testCase.Output, buildName(data, testCase.Format))
}
}

func TestSmbios(t *testing.T) {
dmiEncoded := map[string]interface{}{}
values.PutValue(dmiEncoded, "Myself", "System Information", "Manufacturer")
var buf bytes.Buffer
b64Enc := base64.NewEncoder(base64.StdEncoding, &buf)
json.NewEncoder(b64Enc).Encode(dmiEncoded)
_ = b64Enc.Close()

testCase := []struct {
header http.Header
path []string // Path to the value
value string // Actual value
gotIt bool // Did we get the value
}{
{
http.Header{"X-Cattle-Smbios": {buf.String()}}, // Old header
[]string{"System Information", "Manufacturer"},
"Myself",
true,
},
{
http.Header{"X-Cattle-Smbios-1": {buf.String()}}, // New header
[]string{"System Information", "Manufacturer"},
"Myself",
true,
},
{
http.Header{"X-Cattle-Smbios-2": {buf.String()}}, // New header but missing the first part
[]string{"System Information", "Manufacturer"},
"",
false,
},
{
http.Header{}, // Empty header
[]string{"System Information", "Manufacturer"},
"",
false,
},
}

for _, test := range testCase {
data, err := getSMBios(&http.Request{Header: test.header})
assert.Equal(t, err, nil)
d, gotIt := values.GetValue(data, test.path...)
assert.Equal(t, gotIt, test.gotIt)
// Cant compare string and nil and values.GetValue returns either a string or a nil
if test.value == "" {
assert.Equal(t, d, nil)
} else {
assert.Equal(t, d, test.value)
}

}
}

0 comments on commit 72971ff

Please sign in to comment.