adding two more examples for preservation
This commit is contained in:
		
							
								
								
									
										50
									
								
								cmd/match-path/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cmd/match-path/main.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"regexp" | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func main() { | ||||||
|  | 	policy := "/osb/v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation" | ||||||
|  | 	pathGood := "/osb/v2/service_instances/foo/service_bindings/bar/last_operation" | ||||||
|  | 	pathBad := "/osb/v2/service_instances/foo/evil_path/service_bindings/bar/last_operation" | ||||||
|  | 	pathShort := "/osb/v2/service_instances/foo_bar/" | ||||||
|  |  | ||||||
|  | 	fmt.Printf("Valid Path: %s\n", strconv.FormatBool(checkPolicyMatch(policy, pathGood))) | ||||||
|  | 	fmt.Printf("Valid Path: %s\n", strconv.FormatBool(checkPolicyMatch(policy, pathBad))) | ||||||
|  | 	fmt.Printf("Valid Path: %s\n", strconv.FormatBool(checkPolicyMatch(policy, pathShort))) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func checkPolicyMatch(policy, path string) bool { | ||||||
|  | 	// split the path elements by "/" | ||||||
|  | 	policyElements := strings.Split(policy, "/") | ||||||
|  | 	pathElements := strings.Split(path, "/") | ||||||
|  |  | ||||||
|  | 	// check to see if the number of elements are present in the path to prevent index out of range error | ||||||
|  | 	if len(policyElements) > len(pathElements) { | ||||||
|  | 		return false | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// check each path element | ||||||
|  | 	for i := 0; i < len(policyElements); i++ { | ||||||
|  | 		switch { | ||||||
|  | 		// policy matching wildcard ** | ||||||
|  | 		case regexp.MustCompile(`^\*\*$`).MatchString(policyElements[i]): | ||||||
|  | 			continue | ||||||
|  | 		// policy matching wildcard {foo} | ||||||
|  | 		case regexp.MustCompile(`^\{.+\}$`).MatchString(policyElements[i]): | ||||||
|  | 			continue | ||||||
|  | 		// path matches exactly, should we ignore case with EqualFold() | ||||||
|  | 		case policyElements[i] == pathElements[i]: | ||||||
|  | 			continue | ||||||
|  | 		// default deny | ||||||
|  | 		default: | ||||||
|  | 			return false | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return true | ||||||
|  | } | ||||||
							
								
								
									
										39
									
								
								cmd/random-order/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								cmd/random-order/main.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"hash/crc32" | ||||||
|  | 	"hash/crc64" | ||||||
|  | 	"math/rand" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	names = []string{ | ||||||
|  | 		"John", | ||||||
|  | 		"Paul", | ||||||
|  | 		"Peter", | ||||||
|  | 		"Sam", | ||||||
|  | 		"Susan", | ||||||
|  | 		"Sarah", | ||||||
|  | 		"Melony", | ||||||
|  | 	} | ||||||
|  | 	date = "2024-08-14" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func removeIndex(s []string, i int) []string { | ||||||
|  | 	ret := make([]string, 0) | ||||||
|  | 	ret = append(ret, s[:i]...) | ||||||
|  | 	return append(ret, s[i+1:]...) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func main() { | ||||||
|  | 	seed := crc64.Checksum([]byte(date), crc64.MakeTable(crc32.IEEE)) | ||||||
|  | 	r := rand.New(rand.NewSource((int64(seed)))) | ||||||
|  |  | ||||||
|  | 	totalNames := len(names) - 1 | ||||||
|  | 	for i := 0; i <= totalNames; i++ { | ||||||
|  | 		index := r.Intn(len(names)) | ||||||
|  | 		fmt.Printf("%s\n", names[index]) | ||||||
|  | 		names = removeIndex(names, index) | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user