mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 12:37:48 +00:00
v2 swagger scopes added to what-changed.
Added hashing functions to all v2 and v3 models that handle security, thats the next target.
This commit is contained in:
@@ -559,8 +559,8 @@ func AreEqual(l, r Hashable) bool {
|
||||
func GenerateHashString(v any) string {
|
||||
if h, ok := v.(Hashable); ok {
|
||||
if h != nil {
|
||||
return fmt.Sprintf("%x", h.Hash())
|
||||
return fmt.Sprintf(HASH, h.Hash())
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(v))))
|
||||
return fmt.Sprintf(HASH, sha256.Sum256([]byte(fmt.Sprint(v))))
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
HASH = "%x"
|
||||
)
|
||||
|
||||
// Buildable is an interface for any struct that can be 'built out'. This means that a struct can accept
|
||||
// a root node and a reference to the index that carries data about any references used.
|
||||
//
|
||||
|
||||
@@ -4,10 +4,13 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"gopkg.in/yaml.v3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Scopes is a low-level representation of a Swagger / OpenAPI 2 OAuth2 Scopes object.
|
||||
@@ -31,6 +34,9 @@ func (s *Scopes) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
if utils.IsNodeMap(root) {
|
||||
for k := range root.Content {
|
||||
if k%2 == 0 {
|
||||
if strings.Contains(root.Content[k].Value, "x-") {
|
||||
continue
|
||||
}
|
||||
valueMap[low.KeyReference[string]{
|
||||
Value: root.Content[k].Value,
|
||||
KeyNode: root.Content[k],
|
||||
@@ -44,3 +50,15 @@ func (s *Scopes) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash will return a consistent SHA256 Hash of the Scopes object
|
||||
func (s *Scopes) Hash() [32]byte {
|
||||
var f []string
|
||||
for i := range s.Values {
|
||||
f = append(f, fmt.Sprintf("%s-%s", i.Value, s.Values[i].Value))
|
||||
}
|
||||
for k := range s.Extensions {
|
||||
f = append(f, fmt.Sprintf("%s-%v", k.Value, s.Extensions[k].Value))
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
||||
}
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"gopkg.in/yaml.v3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SecurityScheme is a low-level representation of a Swagger / OpenAPI 2 SecurityScheme object.
|
||||
@@ -38,3 +41,36 @@ func (ss *SecurityScheme) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
ss.Scopes = scopes
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash will return a consistent SHA256 Hash of the SecurityScheme object
|
||||
func (ss *SecurityScheme) Hash() [32]byte {
|
||||
var f []string
|
||||
if !ss.Type.IsEmpty() {
|
||||
f = append(f, ss.Type.Value)
|
||||
}
|
||||
if !ss.Description.IsEmpty() {
|
||||
f = append(f, ss.Description.Value)
|
||||
}
|
||||
if !ss.Name.IsEmpty() {
|
||||
f = append(f, ss.Name.Value)
|
||||
}
|
||||
if !ss.In.IsEmpty() {
|
||||
f = append(f, ss.In.Value)
|
||||
}
|
||||
if !ss.Flow.IsEmpty() {
|
||||
f = append(f, ss.Flow.Value)
|
||||
}
|
||||
if !ss.AuthorizationUrl.IsEmpty() {
|
||||
f = append(f, ss.AuthorizationUrl.Value)
|
||||
}
|
||||
if !ss.TokenUrl.IsEmpty() {
|
||||
f = append(f, ss.TokenUrl.Value)
|
||||
}
|
||||
if !ss.Scopes.IsEmpty() {
|
||||
f = append(f, low.GenerateHashString(ss.Scopes.Value))
|
||||
}
|
||||
for k := range ss.Extensions {
|
||||
f = append(f, fmt.Sprintf("%s-%v", k.Value, ss.Extensions[k].Value))
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
||||
}
|
||||
|
||||
@@ -101,4 +101,5 @@ const (
|
||||
ExplodeLabel = "explode"
|
||||
ContentTypeLabel = "contentType"
|
||||
SecurityDefinitionLabel = "securityDefinition"
|
||||
Scopes = "scopes"
|
||||
)
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
package v3
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"gopkg.in/yaml.v3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// OAuthFlows represents a low-level OpenAPI 3+ OAuthFlows object.
|
||||
@@ -52,7 +55,27 @@ func (o *OAuthFlows) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
}
|
||||
o.AuthorizationCode = v
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash will return a consistent SHA256 Hash of the OAuthFlow object
|
||||
func (o *OAuthFlows) Hash() [32]byte {
|
||||
var f []string
|
||||
if !o.Implicit.IsEmpty() {
|
||||
f = append(f, low.GenerateHashString(o.Implicit.Value))
|
||||
}
|
||||
if !o.Password.IsEmpty() {
|
||||
f = append(f, low.GenerateHashString(o.Password.Value))
|
||||
}
|
||||
if !o.ClientCredentials.IsEmpty() {
|
||||
f = append(f, low.GenerateHashString(o.ClientCredentials.Value))
|
||||
}
|
||||
if !o.AuthorizationCode.IsEmpty() {
|
||||
f = append(f, low.GenerateHashString(o.AuthorizationCode.Value))
|
||||
}
|
||||
for k := range o.Extensions {
|
||||
f = append(f, fmt.Sprintf("%s-%v", k.Value, o.Extensions[k].Value))
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
||||
}
|
||||
|
||||
// OAuthFlow represents a low-level OpenAPI 3+ OAuthFlow object.
|
||||
@@ -80,3 +103,24 @@ func (o *OAuthFlow) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
o.Extensions = low.ExtractExtensions(root)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash will return a consistent SHA256 Hash of the OAuthFlow object
|
||||
func (o *OAuthFlow) Hash() [32]byte {
|
||||
var f []string
|
||||
if !o.AuthorizationUrl.IsEmpty() {
|
||||
f = append(f, o.AuthorizationUrl.Value)
|
||||
}
|
||||
if !o.TokenUrl.IsEmpty() {
|
||||
f = append(f, o.TokenUrl.Value)
|
||||
}
|
||||
if !o.RefreshUrl.IsEmpty() {
|
||||
f = append(f, o.RefreshUrl.Value)
|
||||
}
|
||||
for i := range o.Scopes.Value {
|
||||
f = append(f, fmt.Sprintf("%s-%s", i.Value, o.Scopes.Value[i].Value))
|
||||
}
|
||||
for k := range o.Extensions {
|
||||
f = append(f, fmt.Sprintf("%s-%v", k.Value, o.Extensions[k].Value))
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package v3
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
@@ -66,12 +67,39 @@ func (ss *SecurityScheme) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
if oa.Value != nil {
|
||||
ss.Flows = oa
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash will return a consistent SHA256 Hash of the SecurityScheme object
|
||||
func (ss *SecurityScheme) Hash() [32]byte {
|
||||
var f []string
|
||||
if !ss.Type.IsEmpty() {
|
||||
f = append(f, ss.Type.Value)
|
||||
}
|
||||
if !ss.Description.IsEmpty() {
|
||||
f = append(f, ss.Description.Value)
|
||||
}
|
||||
if !ss.Name.IsEmpty() {
|
||||
f = append(f, ss.Name.Value)
|
||||
}
|
||||
if !ss.In.IsEmpty() {
|
||||
f = append(f, ss.In.Value)
|
||||
}
|
||||
if !ss.Scheme.IsEmpty() {
|
||||
f = append(f, ss.Scheme.Value)
|
||||
}
|
||||
if !ss.BearerFormat.IsEmpty() {
|
||||
f = append(f, ss.BearerFormat.Value)
|
||||
}
|
||||
if !ss.Flows.IsEmpty() {
|
||||
f = append(f, low.GenerateHashString(ss.Flows.Value))
|
||||
}
|
||||
if !ss.OpenIdConnectUrl.IsEmpty() {
|
||||
f = append(f, ss.OpenIdConnectUrl.Value)
|
||||
}
|
||||
for k := range ss.Extensions {
|
||||
f = append(f, fmt.Sprintf("%s-%v", k.Value, ss.Extensions[k].Value))
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
||||
}
|
||||
|
||||
|
||||
@@ -230,6 +230,20 @@ func CheckMapForChanges[T any, R any](expLeft, expRight map[low.KeyReference[str
|
||||
return expChanges
|
||||
}
|
||||
|
||||
//func CompareMapStrings(l, r low.ValueReference[string]) *Change {
|
||||
//
|
||||
// if l.Value != r.Value {
|
||||
// return &Change{
|
||||
// Context: CreateContext(l.ValueNode, r.ValueNode),
|
||||
// ChangeType: Modified,
|
||||
// Original: l.Value,
|
||||
// New: r.Value,
|
||||
// Breaking: false,
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
// ExtractStringValueSliceChanges will compare two low level string slices for changes.
|
||||
func ExtractStringValueSliceChanges(lParam, rParam []low.ValueReference[string], changes *[]*Change, label string) {
|
||||
lKeys := make([]string, len(lParam))
|
||||
|
||||
@@ -241,8 +241,5 @@ func CompareHeaders(l, r any) *HeaderChanges {
|
||||
}
|
||||
CheckProperties(props)
|
||||
hc.Changes = changes
|
||||
if hc.TotalChanges() <= 0 {
|
||||
return nil
|
||||
}
|
||||
return hc
|
||||
}
|
||||
|
||||
61
what-changed/model/scopes.go
Normal file
61
what-changed/model/scopes.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/v2"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
)
|
||||
|
||||
type ScopesChanges struct {
|
||||
PropertyChanges
|
||||
ExtensionChanges *ExtensionChanges
|
||||
}
|
||||
|
||||
func (s *ScopesChanges) TotalChanges() int {
|
||||
c := s.PropertyChanges.TotalChanges()
|
||||
if s.ExtensionChanges != nil {
|
||||
c += s.ExtensionChanges.TotalChanges()
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (s *ScopesChanges) TotalBreakingChanges() int {
|
||||
return s.PropertyChanges.TotalBreakingChanges()
|
||||
}
|
||||
|
||||
func CompareScopes(l, r *v2.Scopes) *ScopesChanges {
|
||||
if low.AreEqual(l, r) {
|
||||
return nil
|
||||
}
|
||||
var changes []*Change
|
||||
for v := range l.Values {
|
||||
if r != nil && r.FindScope(v.Value) == nil {
|
||||
CreateChange(&changes, ObjectRemoved, v3.Scopes,
|
||||
l.Values[v].ValueNode, nil, true,
|
||||
v.Value, nil)
|
||||
continue
|
||||
}
|
||||
if r != nil && r.FindScope(v.Value) != nil {
|
||||
if l.Values[v].Value != r.FindScope(v.Value).Value {
|
||||
CreateChange(&changes, Modified, v3.Scopes,
|
||||
l.Values[v].ValueNode, r.FindScope(v.Value).ValueNode, true,
|
||||
l.Values[v].Value, r.FindScope(v.Value).Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
for v := range r.Values {
|
||||
if l != nil && l.FindScope(v.Value) == nil {
|
||||
CreateChange(&changes, ObjectAdded, v3.Scopes,
|
||||
nil, r.Values[v].ValueNode, false,
|
||||
nil, v.Value)
|
||||
}
|
||||
}
|
||||
|
||||
sc := new(ScopesChanges)
|
||||
sc.Changes = changes
|
||||
sc.ExtensionChanges = CompareExtensions(l.Extensions, r.Extensions)
|
||||
return sc
|
||||
}
|
||||
125
what-changed/model/scopes_test.go
Normal file
125
what-changed/model/scopes_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
v2 "github.com/pb33f/libopenapi/datamodel/low/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareScopes(t *testing.T) {
|
||||
|
||||
left := `pizza: pie
|
||||
lemon: sky
|
||||
x-nugget: chicken`
|
||||
right := `pizza: pie
|
||||
lemon: sky
|
||||
x-nugget: chicken`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Scopes
|
||||
var rDoc v2.Scopes
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareScopes(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
|
||||
func TestCompareScopes_Modified(t *testing.T) {
|
||||
|
||||
left := `pizza: sky
|
||||
lemon: sky
|
||||
x-nugget: chicken`
|
||||
right := `pizza: pie
|
||||
lemon: sky
|
||||
x-nugget: chicken`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Scopes
|
||||
var rDoc v2.Scopes
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareScopes(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
|
||||
assert.Equal(t, Modified, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, "pie", extChanges.Changes[0].New)
|
||||
}
|
||||
|
||||
func TestCompareScopes_Added(t *testing.T) {
|
||||
|
||||
left := `pizza: sky
|
||||
x-nugget: chicken`
|
||||
right := `pizza: sky
|
||||
lemon: sky
|
||||
x-nugget: chicken`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Scopes
|
||||
var rDoc v2.Scopes
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareScopes(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, 0, extChanges.TotalBreakingChanges())
|
||||
assert.Equal(t, ObjectAdded, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, "sky", extChanges.Changes[0].New)
|
||||
assert.Equal(t, "lemon", extChanges.Changes[0].NewObject)
|
||||
}
|
||||
|
||||
func TestCompareScopes_Removed_ChangeExt(t *testing.T) {
|
||||
|
||||
left := `pizza: sky
|
||||
x-nugget: chicken`
|
||||
right := `pizza: sky
|
||||
lemon: sky
|
||||
x-nugget: soup`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Scopes
|
||||
var rDoc v2.Scopes
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareScopes(&rDoc, &lDoc)
|
||||
assert.Equal(t, 2, extChanges.TotalChanges())
|
||||
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
|
||||
assert.Equal(t, ObjectRemoved, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, "sky", extChanges.Changes[0].Original)
|
||||
assert.Equal(t, "lemon", extChanges.Changes[0].OriginalObject)
|
||||
}
|
||||
@@ -346,3 +346,36 @@ biscuit:
|
||||
assert.Equal(t, ObjectRemoved, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, "rich tea", extChanges.Changes[0].Original)
|
||||
}
|
||||
|
||||
func TestCompareSecurityRequirement_Add(t *testing.T) {
|
||||
|
||||
left := `
|
||||
biscuit:
|
||||
- biscotti
|
||||
- rich tea`
|
||||
|
||||
right := `punch:
|
||||
- nice
|
||||
cheese:
|
||||
- pizza
|
||||
- pie
|
||||
biscuit:
|
||||
- biscotti
|
||||
- rich tea`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.SecurityRequirement
|
||||
var rDoc v2.SecurityRequirement
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare
|
||||
extChanges := CompareSecurityRequirement(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user