Skip to content

Commit 589bf7a

Browse files
committed
APIGOV-32421 - strip unwanted tags
1 parent 7026585 commit 589bf7a

7 files changed

Lines changed: 70 additions & 0 deletions

File tree

pkg/apic/servicebody.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type ServiceBody struct {
5555
ardName string
5656
uniqueARD bool
5757
ignoreSpecBasesCreds bool
58+
ignoreSpecTags []string
5859
stripOASExtensions bool
5960
stripOASServersBeforePublish bool
6061
specHash string

pkg/apic/servicebuilder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type ServiceBuilder interface {
5555
AddCredentialRequestDefinition(credentialRequestDefName string) ServiceBuilder
5656
SetAccessRequestDefinitionName(accessRequestDefName string, isUnique bool) ServiceBuilder
5757
SetIgnoreSpecBasedCreds(ignore bool) ServiceBuilder
58+
SetIgnoreSpecTags(tags []string) ServiceBuilder
5859
SetStripOASExtensions(strip bool) ServiceBuilder
5960
SetStripOASServersBeforePublish() ServiceBuilder
6061

@@ -383,6 +384,14 @@ func (b *serviceBodyBuilder) Build() (ServiceBody, error) {
383384
if b.serviceBody.stripOASServersBeforePublish {
384385
b.serviceBody.originalSpecHash = b.serviceBody.specHash
385386
val.stripEndpoints()
387+
}
388+
389+
if len(b.serviceBody.ignoreSpecTags) > 0 {
390+
b.serviceBody.originalSpecHash = b.serviceBody.specHash
391+
val.stripTags(b.serviceBody.ignoreSpecTags)
392+
}
393+
394+
if b.serviceBody.stripOASServersBeforePublish || len(b.serviceBody.ignoreSpecTags) > 0 {
386395
b.serviceBody.SpecDefinition = val.GetSpecBytes()
387396
newHash, _ := util.ComputeHash(val.GetSpecBytes())
388397
b.serviceBody.specHash = fmt.Sprintf("%v", newHash)
@@ -435,6 +444,11 @@ func (b *serviceBodyBuilder) SetIgnoreSpecBasedCreds(ignore bool) ServiceBuilder
435444
return b
436445
}
437446

447+
func (b *serviceBodyBuilder) SetIgnoreSpecTags(tags []string) ServiceBuilder {
448+
b.serviceBody.ignoreSpecTags = tags
449+
return b
450+
}
451+
438452
func (b *serviceBodyBuilder) SetStripOASServersBeforePublish() ServiceBuilder {
439453
b.serviceBody.stripOASServersBeforePublish = true
440454
return b

pkg/apic/servicebuilder_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestServiceBodySetters(t *testing.T) {
8585
SetIgnoreSpecBasedCreds(true).
8686
SetStripOASExtensions(true).
8787
SetStripOASServersBeforePublish().
88+
SetIgnoreSpecTags([]string{"tag1"}).
8889
SetInstanceLifecycle("stage", "active", "").
8990
SetServiceEndpoints(ep)
9091

@@ -142,6 +143,7 @@ func TestServiceBodySetters(t *testing.T) {
142143
assert.NotNil(t, instanceLifecycle)
143144
assert.Equal(t, "stage", instanceLifecycle.Stage)
144145
assert.Equal(t, "active", instanceLifecycle.ReleaseState.Name)
146+
assert.Equal(t, []string{"tag1"}, sb.ignoreSpecTags)
145147

146148
sb, err = serviceBuilder.
147149
SetSourceDataplaneType(GitHub, true).
@@ -262,4 +264,21 @@ func TestServiceBodyBuilderWithLargeSpec(t *testing.T) {
262264
assert.Nil(t, err)
263265
assert.NotNil(t, sb)
264266
assert.NotEqual(t, sb.originalSpecHash, sb.specHash)
267+
268+
// SetIgnoreSpecTags strips matching top-level OAS tags from the spec before publishing.
269+
// The original hash is computed before stripping, so it must differ from the final spec hash.
270+
sb, err = serviceBuilder.
271+
SetIgnoreSpecTags([]string{"pet", "store", "user"}).
272+
Build()
273+
assert.Nil(t, err)
274+
assert.NotNil(t, sb)
275+
assert.NotEqual(t, sb.originalSpecHash, sb.specHash)
276+
277+
// A tag name that does not exist in the spec leaves hashes equal (spec is unchanged).
278+
sb, err = serviceBuilder.
279+
SetIgnoreSpecTags([]string{"nonexistent-tag"}).
280+
Build()
281+
assert.Nil(t, err)
282+
assert.NotNil(t, sb)
283+
assert.Equal(t, sb.originalSpecHash, sb.specHash)
265284
}

pkg/apic/specoas2processor.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ func (p *oas2SpecProcessor) stripEndpoints() {
8585
p.spec.Schemes = []string{}
8686
}
8787

88+
func (p *oas2SpecProcessor) stripTags(tags []string) {
89+
if len(tags) == 0 {
90+
return
91+
}
92+
for _, tagName := range tags {
93+
for i := len(p.spec.Tags) - 1; i >= 0; i-- {
94+
if p.spec.Tags[i].Name == tagName {
95+
p.spec.Tags = append(p.spec.Tags[:i], p.spec.Tags[i+1:]...)
96+
}
97+
}
98+
}
99+
}
100+
88101
func (p *oas2SpecProcessor) ParseAuthInfo() {
89102
authPolicies := []string{}
90103
keyInfo := []APIKeyInfo{}

pkg/apic/specoas3processor.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,19 @@ func (p *oas3SpecProcessor) stripEndpoints() {
285285
p.spec.Servers = []*openapi3.Server{}
286286
}
287287

288+
func (p *oas3SpecProcessor) stripTags(tags []string) {
289+
if len(tags) == 0 {
290+
return
291+
}
292+
for _, tagName := range tags {
293+
for i := len(p.spec.Tags) - 1; i >= 0; i-- {
294+
if p.spec.Tags[i].Name == tagName {
295+
p.spec.Tags = append(p.spec.Tags[:i], p.spec.Tags[i+1:]...)
296+
}
297+
}
298+
}
299+
}
300+
288301
func (p *oas3SpecProcessor) GetSpecBytes() []byte {
289302
s, _ := json.Marshal(p.spec)
290303
return s

pkg/apic/specparser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type OasSpecProcessor interface {
5858
GetResourceType() string
5959
GetVersion() string
6060
stripEndpoints()
61+
stripTags([]string)
6162
}
6263

6364
// SpecResourceParser -

pkg/config/centralconfig.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ type CentralConfig interface {
228228
GetManagedEnvironments() []string
229229
GetProvisioningRetryCount() int
230230
IsInstanceValidationEnabled() bool
231+
GetIgnoreSpecTags() []string
231232
}
232233

233234
// CentralConfiguration - Structure to hold the central config
@@ -268,6 +269,7 @@ type CentralConfiguration struct {
268269
CredentialConfig CredentialConfig `config:"credential"`
269270
ProvisioningRetryCount int `config:"provisioningRetryCount"`
270271
InstanceValidatorEnabled bool `config:"instanceValidatorEnabled"`
272+
IgnoreSpecTags []string `config:"ignoreSpecTags"`
271273
managedEnvironments []string
272274
JobExecutionTimeout time.Duration
273275
environmentID string
@@ -695,6 +697,10 @@ func (c *CentralConfiguration) IsInstanceValidationEnabled() bool {
695697
return c.InstanceValidatorEnabled
696698
}
697699

700+
func (c *CentralConfiguration) GetIgnoreSpecTags() []string {
701+
return c.IgnoreSpecTags
702+
}
703+
698704
const (
699705
pathRegion = "central.region"
700706
pathTenantID = "central.organizationID"
@@ -740,6 +746,7 @@ const (
740746
pathProvisioningRetryCount = "central.provisioningRetryCount"
741747
pathErrorSamplingEnabled = "central.errorSamplingEnabled"
742748
pathInstanceValidatorEnabled = "central.instanceValidatorEnabled"
749+
pathIgnoreSpecTags = "central.ignoreSpecTags"
743750
)
744751

745752
// ValidateCfg - Validates the config, implementing IConfigInterface
@@ -933,6 +940,7 @@ func AddCentralConfigProperties(props properties.Properties, agentType AgentType
933940
AddUsageReportingProperties(props)
934941
props.AddBoolProperty(pathErrorSamplingEnabled, false, "Controls whether error sampling is enabled")
935942
} else {
943+
props.AddStringSliceProperty(pathIgnoreSpecTags, []string{}, "Controls whether an agent has instance validation enabled")
936944
props.AddStringProperty(pathAdditionalTags, "", "Additional Tags to Add to discovered APIs when publishing to Amplify Central")
937945
props.AddBoolProperty(pathAppendEnvironmentToTitle, true, "When true API titles and descriptions will be appended with environment name")
938946
props.AddIntProperty(pathProvisioningRetryCount, 0, "The number of retries, in case it fails, for any provisioning event", properties.WithUpperLimitInt(3))
@@ -1015,6 +1023,7 @@ func ParseCentralConfig(props properties.Properties, agentType AgentType) (Centr
10151023
cfg.MigrationSettings = ParseMigrationConfig(props)
10161024
cfg.CredentialConfig = newCredentialConfig()
10171025
cfg.CredentialConfig.SetAllowedOAuthMethods(props.StringSlicePropertyValue(pathCredentialsOAuthMethods))
1026+
cfg.IgnoreSpecTags = props.StringSlicePropertyValue(pathIgnoreSpecTags)
10181027
}
10191028
if cfg.AgentName == "" && cfg.Environment != "" && agentType.ToShortString() != "" {
10201029
cfg.AgentName = cfg.Environment + "-" + agentType.ToShortString()

0 commit comments

Comments
 (0)