Skip to content

Commit 72971ff

Browse files
author
Itxaka
authored
Backwards compatibility for smbios headers (#137)
1 parent be788bc commit 72971ff

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

pkg/server/register.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,21 @@ func buildName(data map[string]interface{}, name string) string {
245245

246246
func getSMBios(req *http.Request) (map[string]interface{}, error) {
247247
var smbios string
248-
// 200 * 875bytes per header = 175Kb of smbios data, should be enough?
249-
for i := 1; i <= 200; i++ {
250-
header := req.Header.Get(fmt.Sprintf("X-Cattle-Smbios-%d", i))
251-
if header == "" {
252-
break
248+
// Old header sent by clients on commit < be788bcfd899977770d84c996abd967c30942822
249+
headerOld := req.Header.Get("X-Cattle-Smbios")
250+
251+
// If old header not found try to get the new ones
252+
if headerOld == "" {
253+
// 200 * 875bytes per header = 175Kb of smbios data, should be enough?
254+
for i := 1; i <= 200; i++ {
255+
header := req.Header.Get(fmt.Sprintf("X-Cattle-Smbios-%d", i))
256+
if header == "" {
257+
break
258+
}
259+
smbios = smbios + header
253260
}
254-
smbios = smbios + header
261+
} else {
262+
smbios = headerOld
255263
}
256264

257265
if smbios == "" {

pkg/server/register_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ limitations under the License.
1717
package server
1818

1919
import (
20+
"bytes"
21+
"encoding/base64"
22+
"encoding/json"
23+
"net/http"
2024
"testing"
2125

26+
values "github.com/rancher/wrangler/pkg/data"
2227
"gotest.tools/assert"
2328
)
2429

@@ -95,3 +100,58 @@ func TestBuildName(t *testing.T) {
95100
assert.Equal(t, testCase.Output, buildName(data, testCase.Format))
96101
}
97102
}
103+
104+
func TestSmbios(t *testing.T) {
105+
dmiEncoded := map[string]interface{}{}
106+
values.PutValue(dmiEncoded, "Myself", "System Information", "Manufacturer")
107+
var buf bytes.Buffer
108+
b64Enc := base64.NewEncoder(base64.StdEncoding, &buf)
109+
json.NewEncoder(b64Enc).Encode(dmiEncoded)
110+
_ = b64Enc.Close()
111+
112+
testCase := []struct {
113+
header http.Header
114+
path []string // Path to the value
115+
value string // Actual value
116+
gotIt bool // Did we get the value
117+
}{
118+
{
119+
http.Header{"X-Cattle-Smbios": {buf.String()}}, // Old header
120+
[]string{"System Information", "Manufacturer"},
121+
"Myself",
122+
true,
123+
},
124+
{
125+
http.Header{"X-Cattle-Smbios-1": {buf.String()}}, // New header
126+
[]string{"System Information", "Manufacturer"},
127+
"Myself",
128+
true,
129+
},
130+
{
131+
http.Header{"X-Cattle-Smbios-2": {buf.String()}}, // New header but missing the first part
132+
[]string{"System Information", "Manufacturer"},
133+
"",
134+
false,
135+
},
136+
{
137+
http.Header{}, // Empty header
138+
[]string{"System Information", "Manufacturer"},
139+
"",
140+
false,
141+
},
142+
}
143+
144+
for _, test := range testCase {
145+
data, err := getSMBios(&http.Request{Header: test.header})
146+
assert.Equal(t, err, nil)
147+
d, gotIt := values.GetValue(data, test.path...)
148+
assert.Equal(t, gotIt, test.gotIt)
149+
// Cant compare string and nil and values.GetValue returns either a string or a nil
150+
if test.value == "" {
151+
assert.Equal(t, d, nil)
152+
} else {
153+
assert.Equal(t, d, test.value)
154+
}
155+
156+
}
157+
}

0 commit comments

Comments
 (0)