Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAPI security (rebase) #59

Merged
merged 1 commit into from
Dec 22, 2021
Merged

Add OpenAPI security (rebase) #59

merged 1 commit into from
Dec 22, 2021

Conversation

gcasar
Copy link
Contributor

@gcasar gcasar commented Jun 21, 2021

Fixes Issue #35, continues stale PR #50.

I tried to keep the comments from #50 relevant by resolving conflicts by back-merging and addressing them here. We can squash commits at your discretion.

  • adds generator methods
  • some edge case test coverage
  • marshalling empty security field in path section (see security parameter - an empty array is used to signal "no security" override)

The last point is the only one that is troublesome since it does not align with the way JSON gets serialised and would benefit from something like this request. The proposed way is backwards compatible but does repeat some code.

@wI2L
Copy link
Owner

wI2L commented Jun 22, 2021

@gcasar Thanks for picking this up. I'll review it asap this week.

Regarding the omitnil proposal you linked, you could check out my other package jettison. (which support it), but after a quick peek to your custom marshaller implem, I think it's better than introducing a new dependency just for that need (which would only be for JSON marshaling anyway, and not YAML).

openapi/spec.go Outdated
}

// A workaround for missing omitnil functionality.
// explicitly excludes Security which:
Copy link
Owner

@wI2L wI2L Jun 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rephrase the comment to smthing like this, up to you tho:

It explicitely omit the Security field from marshaling when it is nil, but not when empty.

@@ -104,6 +104,12 @@ func (g *Generator) SetServers(servers []*Server) {
g.api.Servers = servers
}

// SetSecurity sets the security options for the
// current specification.
func (g *Generator) SetSecurity(security *SecurityRequirement) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to name the function SetSecurityRequirement, to be in-par with the OpenAPI spec object naming.

fizz.go Outdated
}

// NoSecurity explicitly removes all security.
func NoSecurity() func(*openapi.OperationInfo) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this feels weird for the end-user. I'm wondering if it shouldn't be the default, applied directly by the package (i.e, if operation.Security == nil, initialize Security to empty slice, via MarshalYAML/JSON) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... given that I understand your comment correctly I believe that would actually change semantics:

  • you want to omit Security altogether (as per the spec) if you are not overriding the top level Security setting
  • any serialized Security value, even an empty array has meaning.

From Operation Object:

A declaration of which security mechanisms can be used for this operation. The list of values includes alternative security requirement objects that can be used. Only one of the security requirement objects need to be satisfied to authorize a request. To make security optional, an empty security requirement ({}) can be included in the array. This definition overrides any declared top-level security. To remove a top-level security declaration, an empty array can be used.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that was me, I didn't read the spec properly. NoSecurity() would be useful only if the top-level Security field is set:

To remove a top-level security declaration, an empty array can be used.

What would you think of WithoutSecurity instead as name ?


Speaking of that, the spec also specify the following:

To make security optional, an empty security requirement ({}) can be included in the array.

We could provide a "helper-func" (WithOptionalSecurity()) for that, that would append an empty openapi.SecurityRequirement object in the operation's Security field (slice). Thoughts ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 👍

@@ -353,6 +353,20 @@ func InputModel(model interface{}) func(*openapi.OperationInfo) {
}
}

// Security adds a security definition to an operation.
func Security(security *openapi.SecurityRequirement) func(*openapi.OperationInfo) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we're including security-related helpers functions such as WithoutSecurity() and OptionalSecurity(), they should also be documented/mentionned.

@wI2L
Copy link
Owner

wI2L commented Jun 29, 2021

@gcasar Sorry for the delay, just got back from holidays. Please see the minor fixes suggestions, otherwise looks good.
I'd rather squash indeed, for commit logs brevity.

@wI2L
Copy link
Owner

wI2L commented Jun 29, 2021

Closes #50.

@wI2L wI2L mentioned this pull request Jun 29, 2021
@gcasar gcasar requested a review from wI2L June 29, 2021 14:15
Copy link
Owner

@wI2L wI2L left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
Few docs fixes proposed, but otherwise LGTM.
Please squash your commits, and I'll merge it ASAP (might take some time before to fix the CI/CD workflow to switch from Jenkins to GitHub Actions).

README.md Outdated
@@ -96,6 +96,17 @@ fizz.Header(name, desc string, model interface{})
// Override the binding model of the operation.
fizz.InputModel(model interface{})

// Add a security requirement to the list of security overrides for this operation.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds redundant to me.

What do you think of:

Overrides top-level security requirement for this operation. Note that this function can be used more than once to add may requirements.

README.md Outdated
// that might have been specified in the root `security` property.
fizz.Security(security *openapi.SecurityRequirement)

// Make other security requirements optional.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think it is better to be explicit. We should indicate what the function does, not just its effect:

Add an empty security requirement to this operation to make other security requirements optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure how to break this one - let me know if you want me to reformat it:)

README.md Outdated
// Make other security requirements optional.
fizz.WithOptionalSecurity()

// Remove any security requirements for this operation.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove any top-level security requirements for this operation.

fizz.go Outdated
}
}

// WithoutSecurity explicitly removes all security.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use same comment as those in README. See proposal.

fizz.go Outdated
}
}

// WithOptionalSecurity makes other security requirements optional.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use same comment as those in README. See proposal.

@gcasar
Copy link
Contributor Author

gcasar commented Jun 29, 2021

@wI2L almost there :) I don't like to squash by force pushing - is it ok with you if you squash it yourself when merging?

@gcasar gcasar requested a review from wI2L June 29, 2021 19:13
@ipfans
Copy link
Contributor

ipfans commented Aug 8, 2021

Any updates @wI2L ?

@wI2L
Copy link
Owner

wI2L commented Aug 20, 2021

@ipfans I'll take care of migrating CI to Github actions and merge pending pull requests to make a proper release by september. Sorry for the delay.

@nikicc
Copy link
Contributor

nikicc commented Nov 19, 2021

Any updates on this? I'd find it useful 😊

@codecov
Copy link

codecov bot commented Dec 22, 2021

Codecov Report

Merging #59 (2994b14) into master (8a048be) will decrease coverage by 0.71%.
The diff coverage is 72.41%.

❗ Current head 2994b14 differs from pull request most recent head c029d2c. Consider uploading reports for the commit c029d2c to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
- Coverage   95.88%   95.17%   -0.72%     
==========================================
  Files           7        7              
  Lines         924      953      +29     
==========================================
+ Hits          886      907      +21     
- Misses         22       30       +8     
  Partials       16       16              
Impacted Files Coverage Δ
openapi/generator.go 94.25% <50.00%> (-0.14%) ⬇️
openapi/spec.go 81.57% <65.00%> (-18.43%) ⬇️
fizz.go 98.66% <100.00%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8a048be...c029d2c. Read the comment docs.

@wI2L wI2L force-pushed the openapi-security branch 5 times, most recently from 285ce19 to 7606e1d Compare December 22, 2021 15:49
wI2L
wI2L previously approved these changes Dec 22, 2021
Co-authored-by: Gregor Casar <gregorcasar@gmail.com>
@wI2L wI2L merged commit 14ef325 into wI2L:master Dec 22, 2021
@wI2L
Copy link
Owner

wI2L commented Dec 22, 2021

To everyone involved on this PR, and expecting the changes, I apologize for the unreasonable delay, haven't found some time to work on it, and I wanted to update the CI pipeline of the project before merhing anything new. This will be shipped in next release.

@nikicc
Copy link
Contributor

nikicc commented Dec 22, 2021

Thanks for getting it in @wI2L!

@nikicc nikicc mentioned this pull request Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create methods for OpenAPI security
5 participants