Skip to content

Commit

Permalink
Fixed filtering by xname in endpoint-history requests (#58)
Browse files Browse the repository at this point in the history
- Searching with an xname prefix and no endpoint type would return
  everything that matched the prefix. With this change it now will only
  match the xname exactly and not by prefix.
- Searching with a bad endpoint type would return a result. With this
  change it will return an empty list.

CASMHMS-5812
CASMHMS-5813
  • Loading branch information
shunr-hpe authored Apr 14, 2023
1 parent a7ed452 commit c682c2e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
kubernetes/.packaged/
.idea/
*.swp

# Prevent certificates from getting checked in
*.ca
Expand All @@ -8,3 +9,6 @@ kubernetes/.packaged/
*.csr
*.key
*.pem

# compiled go command
boot-script-service
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.23.0
1.24.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.24.0] - 2023-03-28

### Changed

- CASMHMS-5812 and CASMHMS-5813: Fixed the endpoint-history API to filter data correctly when using the name and endpoint query parameters.

## [1.23.0] - 2023-01-24

### Changed
Expand Down
22 changes: 19 additions & 3 deletions cmd/boot-script-service/boot_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,13 @@ func searchKeyspace(prefix string) ([]hmetcd.Kvi_KV, error) {
// > If range_end is key plus one (e.g., "aa"+1 == "ab", "a\xff"+1 == "b"), then the range request gets all keys
// > prefixed with key.
// https://github.com/etcd-io/etcd/pull/7206/commits/7e31ddd32a4511c436b14e30ef43756ac782d080

rangeStart := prefix
rangePrefix := prefix[:len(prefix)-1]
rangeLastNextChar := prefix[len(prefix)-1:][0] + 1
rangeEnd := fmt.Sprintf("%s%c", rangePrefix, rangeLastNextChar)

return kvstore.GetRange(rangePrefix, rangeEnd)
return kvstore.GetRange(rangeStart, rangeEnd)
}

func getAccessesForPrefix(prefix string) (accesses []bssTypes.EndpointAccess, err error) {
Expand Down Expand Up @@ -681,15 +683,29 @@ func getAccessesForPrefix(prefix string) (accesses []bssTypes.EndpointAccess, er
func SearchEndpointAccessed(name string, endpointType bssTypes.EndpointType) (accesses []bssTypes.EndpointAccess,
err error) {
if name == "" && endpointType == "" {
return getAccessesForPrefix(endpointAccessPfx)
return getAccessesForPrefix(fmt.Sprintf("%s/", endpointAccessPfx))
} else if name != "" && endpointType == "" {
return getAccessesForPrefix(fmt.Sprintf("%s/%s", endpointAccessPfx, name))
return getAccessesForPrefix(fmt.Sprintf("%s/%s/", endpointAccessPfx, name))
} else if name != "" && endpointType != "" {
var epoch int64
epoch, err = getEndpointAccessed(name, endpointType)
if err != nil {
return
}
// epoch == 0 means the given name and endpoint combo has never been accessed.
// A long existing bug/feature of bss has been to return a value in this case with a LastEpoch value of zero.
// The following preserves that behavior, but only if the endpoint type is valid.
if epoch == 0 {
hasValidType := false
for _, t := range bssTypes.EndpointTypes {
if strings.EqualFold(string(endpointType), string(t)) {
hasValidType = true
}
}
if !hasValidType {
return
}
}

access := bssTypes.EndpointAccess{
Name: name,
Expand Down
7 changes: 6 additions & 1 deletion pkg/bssTypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ type EndpointType string

const (
EndpointTypeBootscript EndpointType = "bootscript"
EndpointTypeUserData = "user-data"
EndpointTypeUserData EndpointType = "user-data"
)

var EndpointTypes = []EndpointType{
EndpointTypeBootscript,
EndpointTypeUserData,
}

type EndpointAccess struct {
Name string `json:"name"`
Endpoint EndpointType `json:"endpoint"`
Expand Down
67 changes: 63 additions & 4 deletions test/ct/api/3-destructive/test_endpoint_history.tavern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ stages:
response:
status_code: 200

# Please note that this stage is a hack to retrieve and save the IP address of this container into
# Please note that this stage is a hack to retrieve and save the IP address of this container into
# a variable. Perhaps a plugin might be better here.
- name: Retrieve test container IP address
request:
# The endpoint that we hit doesn't really matter, just
# The endpoint that we hit doesn't really matter, just
url: "{bss_base_url}/boot/v1/service/status"
method: GET
verify: !bool "{verify}"
Expand Down Expand Up @@ -135,9 +135,39 @@ stages:
- bootscript
last_epoch:
type: int
range:
range:
min: 0 # If the epoch time is 0, then that means the endpoint wasn't really queried.

# Verify that there are no matches when using a bad endpoint.
- name: Query endpoint-history with bad endpoint
request:
url: "{bss_base_url}/boot/v1/endpoint-history"
method: GET
verify: !bool "{verify}"
params:
name: "{node_xname}"
endpoint: bootscript-bad
response:
status_code: 200
verify_response_with:
function: tavern.testutils.helpers:validate_pykwalify
extra_kwargs:
schema:
type: seq
matching: all
range:
min: 0
max: 0
sequence:
- type: map
mapping:
name:
type: str
endpoint:
type: str
last_epoch:
type: int

- name: Query for cloud-init user-data for the node
request:
url: "{bss_base_url}/user-data"
Expand Down Expand Up @@ -179,9 +209,38 @@ stages:
- user-data
last_epoch:
type: int
range:
range:
min: 1 # If the epoch time is 0, then that means the endpoint wasn't really queried.

# Verify that there are no matches when using a prefix of the xname.
- name: Query endpoint-history with xname prefix
request:
url: "{bss_base_url}/boot/v1/endpoint-history"
method: GET
verify: !bool "{verify}"
params:
name: x1000
response:
status_code: 200
verify_response_with:
function: tavern.testutils.helpers:validate_pykwalify
extra_kwargs:
schema:
type: seq
matching: all
range:
min: 0
max: 0
sequence:
- type: map
mapping:
name:
type: str
endpoint:
type: str
last_epoch:
type: int

- name: Remove test HSM EthernetInterfaces for the CT Test container
request:
url: "{hsm_base_url}/hsm/v2/Inventory/EthernetInterfaces/0efffffffffe"
Expand Down

0 comments on commit c682c2e

Please sign in to comment.