Skip to content

Commit

Permalink
Merge pull request #43
Browse files Browse the repository at this point in the history
Added the referral token
  • Loading branch information
shunr-hpe authored Jan 20, 2022
2 parents 3006ff6 + 01d0c01 commit f150f14
Show file tree
Hide file tree
Showing 37 changed files with 1,131 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .license_check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_exclude:
- ".license_check.yaml"
- "Jenkinsfile.github"
- "api/swagger.yaml"
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.14.0
1.15.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.15.0] - 2022-01-07

### Changed

- CASMHMS-4903 Added BSS-Referral-Token to POST and PUT for boot parameters

## [1.14.0] - 2021-12-22

### Changed
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# (C) Copyright 2021 Hewlett Packard Enterprise Development LP
# (C) Copyright [2021-2022] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
Expand All @@ -14,7 +14,7 @@
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Expand Down
10 changes: 10 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ paths:
responses:
'201':
description: successfully created boot parameters
headers:
BSS-Referral-Token:
schema:
type: string
description: The UUID that will be included in the boot script. A new UUID is generated on each POST and PUT request.
'400':
description: Bad Request - Invalid BootParams value
schema:
Expand Down Expand Up @@ -405,6 +410,11 @@ paths:
responses:
'200':
description: successfully update boot parameters
headers:
BSS-Referral-Token:
schema:
type: string
description: The UUID that will be included in the boot script. A new UUID is generated on each POST and PUT request.
'400':
description: Bad Request - Invalid BootParams value
schema:
Expand Down
21 changes: 21 additions & 0 deletions build_tag_push.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
#!/usr/bin/env bash
# MIT License
#
# (C) Copyright [2022] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

IMAGE_NAME="cray-bss"

Expand Down
43 changes: 27 additions & 16 deletions cmd/boot-script-service/boot_data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// (C) Copyright [2021] Hewlett Packard Enterprise Development LP
// (C) Copyright [2021-2022] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -45,6 +45,7 @@ import (
"github.com/Cray-HPE/hms-bss/pkg/bssTypes"
hmetcd "github.com/Cray-HPE/hms-hmetcd"
jsonpatch "github.com/evanphx/json-patch"
"github.com/google/uuid"
)

const (
Expand All @@ -57,10 +58,11 @@ const (
)

type BootDataStore struct {
Params string `json:"params,omitempty"`
Kernel string `json:"kernel,omitempty"` // Image storage key
Initrd string `json:"initrd,omitempty"` // Image storage key
CloudInit bssTypes.CloudInit `json:"cloud-init,omitempty"` // Image storage key
Params string `json:"params,omitempty"`
Kernel string `json:"kernel,omitempty"` // Image storage key
Initrd string `json:"initrd,omitempty"` // Image storage key
CloudInit bssTypes.CloudInit `json:"cloud-init,omitempty"` // Image storage key
ReferralToken string `json:"referral-token,omitempty` // UUID
}

type ImageData struct {
Expand All @@ -69,10 +71,11 @@ type ImageData struct {
}

type BootData struct {
Params string
Kernel ImageData
Initrd ImageData
CloudInit bssTypes.CloudInit `json:"cloud-init,omitempty"`
Params string
Kernel ImageData
Initrd ImageData
CloudInit bssTypes.CloudInit
ReferralToken string
}

const DefaultTag = "Default"
Expand Down Expand Up @@ -336,7 +339,7 @@ func extractParamName(x hmetcd.Kvi_KV) (ret string) {
return ret
}

func StoreNew(bp bssTypes.BootParams) error {
func StoreNew(bp bssTypes.BootParams) (error, string) {
item := ""
// Go through the entire struct. We must be storing to new hosts or this
// request must fail.
Expand Down Expand Up @@ -381,28 +384,31 @@ func StoreNew(bp bssTypes.BootParams) error {
}
}
if item != "" {
return fmt.Errorf("Already exists: %s", item)
return fmt.Errorf("Already exists: %s", item), ""
} else {
return Store(bp)
}
}

func Store(bp bssTypes.BootParams) error {
func Store(bp bssTypes.BootParams) (error, string) {
debugf("Store(%v)\n", bp)

var kernel_id, initrd_id string
if bp.Kernel != "" {
kernel_id = imageStore(bp.Kernel, kernelImageType)
if kernel_id == "" {
return fmt.Errorf("Cannot store image path %s", bp.Kernel)
return fmt.Errorf("Cannot store image path %s", bp.Kernel), ""
}
}
if bp.Initrd != "" {
initrd_id = imageStore(bp.Initrd, initrdImageType)
if initrd_id == "" {
return fmt.Errorf("Cannot store image path %s", bp.Initrd)
return fmt.Errorf("Cannot store image path %s", bp.Initrd), ""
}
}
bd := BootDataStore{bp.Params, kernel_id, initrd_id, bp.CloudInit}

referralToken := uuid.New().String()
bd := BootDataStore{bp.Params, kernel_id, initrd_id, bp.CloudInit, referralToken}
var err error
switch {
case len(bp.Hosts) > 0:
Expand Down Expand Up @@ -452,13 +458,17 @@ func Store(bp bssTypes.BootParams) error {
idata := ImageData{bp.Kernel, bp.Params}
debugf("Ready to store data: %s, %v\n", kernel_id, idata)
err = storeData(kernel_id, idata)
referralToken = "" // referralToken was not needed
case initrd_id != "":
err = storeData(initrd_id, ImageData{bp.Initrd, bp.Params})
referralToken = "" // referralToken was not needed
default:
herr := base.NewHMSError("Storage", "Nothing to Store")
herr.AddProblem(base.NewProblemDetailsStatus("Nothing to Store", http.StatusBadRequest))
referralToken = "" // referralToken was not needed
}
return err
debugf("Store referralToken: %s\n", referralToken)
return err, referralToken
}

// The update function will update entries but not NULL out existing entries.
Expand Down Expand Up @@ -841,6 +851,7 @@ func bdConvertUsingImageCache(bds BootDataStore, kernelImages map[string]ImageDa
func bdConvert(bds BootDataStore) (ret BootData) {
ret.Params = bds.Params
ret.CloudInit = bds.CloudInit
ret.ReferralToken = bds.ReferralToken
if bds.Kernel != "" {
imdata, err := getImage(bds.Kernel, "")
if err == nil {
Expand Down
11 changes: 8 additions & 3 deletions cmd/boot-script-service/bss_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// (C) Copyright [2021] Hewlett Packard Enterprise Development LP
// (C) Copyright [2021-2022] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -30,11 +30,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/Cray-HPE/hms-bss/pkg/bssTypes"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/Cray-HPE/hms-bss/pkg/bssTypes"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -121,9 +122,13 @@ func TestStoreAndLookup(t *testing.T) {
{Initrd: "/test/path/initrd.gz", Params: "def-initrd"},
}
for _, bp := range tables {
err := Store(bp)
err, referralToken := Store(bp)
if err != nil {
t.Errorf("Store failed for '%v': %s", bp, err.Error())
} else if referralToken == "" && (bp.Hosts != nil || bp.Nids != nil || bp.Macs != nil) {
t.Errorf("Store failed to create a referral token for '%v'", bp)
} else if referralToken != "" && (bp.Hosts == nil && bp.Nids == nil && bp.Macs == nil) {
t.Errorf("Store incorrectly created a referral token when only setting the kernel or initrd values for '%v'", bp)
}
}

Expand Down
23 changes: 17 additions & 6 deletions cmd/boot-script-service/default_api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// (C) Copyright [2021] Hewlett Packard Enterprise Development LP
// (C) Copyright [2021-2022] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -75,8 +75,9 @@ var gwURI = getEnvVal("BSS_GW_URI", "/apis/bss")
var s3Client *hms_s3.S3Client

type scriptParams struct {
xname string
nid string
xname string
nid string
referralToken string
}

// Note that we allow an empty string if the env variable is defined as such.
Expand Down Expand Up @@ -371,10 +372,13 @@ func BootparametersPost(w http.ResponseWriter, r *http.Request) {
return
}
debugf("Received boot parameters: %v\n", args)
err = StoreNew(args)
err, referralToken := StoreNew(args)
if err == nil {
LogBootParameters("/bootparameters POST", args)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if referralToken != "" {
w.Header().Set("BSS-Referral-Token", referralToken)
}
w.WriteHeader(http.StatusCreated)
} else {
LogBootParameters(fmt.Sprintf("/bootparameters POST FAILED: %s", err.Error()), args)
Expand All @@ -395,10 +399,13 @@ func BootparametersPut(w http.ResponseWriter, r *http.Request) {
return
}
debugf("Received boot parameters: %v\n", args)
err = Store(args)
err, referralToken := Store(args)
if err == nil {
LogBootParameters("/bootparameters PUT", args)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if referralToken != "" {
w.Header().Set("BSS-Referral-Token", referralToken)
}
w.WriteHeader(http.StatusOK)
} else {
LogBootParameters(fmt.Sprintf("/bootparameters PATCH FAILED: %s", err.Error()), args)
Expand Down Expand Up @@ -540,6 +547,10 @@ func buildBootScript(bd BootData, sp scriptParams, chain, descr string) (string,
// Check for special boot parameters.
params = checkParam(params, "xname=", sp.xname)
params = checkParam(params, "nid=", sp.nid)
if sp.referralToken != "" {
params = checkParam(params, "bss_referral_token=", sp.referralToken)
}

// Inject the cloud init address info into the kernel params. If the target
// image does not have cloud-init enabled this wont hurt anything.
// If it does, it tells it to come back to us for the cloud-init meta-data
Expand Down Expand Up @@ -752,7 +763,7 @@ func BootscriptGet(w http.ResponseWriter, r *http.Request) {
if mac == "" && comp.Mac != nil {
mac = comp.Mac[0]
}
sp := scriptParams{comp.ID, comp.NID.String()}
sp := scriptParams{comp.ID, comp.NID.String(), bd.ReferralToken}
chain := "chain " + chainProto + "://" + ipxeServer + gwURI + r.URL.Path
if mac != "" {
chain += "?mac=" + mac
Expand Down
23 changes: 22 additions & 1 deletion dev/etcd/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
#!/bin/bash
# MIT License
#
# (C) Copyright [2022] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

NODE_IP="0.0.0.0"

Expand All @@ -12,4 +33,4 @@ docker run \
--data-dir=/etcd-data --name node1 \
--initial-advertise-peer-urls http://${NODE_IP}:2380 --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://${NODE_IP}:2379 --listen-client-urls http://0.0.0.0:2379 \
--initial-cluster node1=http://${NODE_IP}:2380
--initial-cluster node1=http://${NODE_IP}:2380
21 changes: 21 additions & 0 deletions dev/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
#!/bin/sh
# MIT License
#
# (C) Copyright [2022] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

go run github.com/Cray-HPE/hms-bss/cmd/boot-script-service --cloud-init-address 0.0.0.0:27778 --datastore http://0.0.0.0:2379 --hsm http://0.0.0.0:8000
22 changes: 22 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# MIT License
#
# (C) Copyright [2022] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

SDIR:=$(strip $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/../../api)
ODIR:=$(strip $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))))
DIR:=$(strip $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))))
Expand Down
Loading

0 comments on commit f150f14

Please sign in to comment.