-
Notifications
You must be signed in to change notification settings - Fork 103
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
REP-7789 Added Rax Roles extension to OpenApi #2070
base: master
Are you sure you want to change the base?
Changes from 1 commit
20dbf62
c04ba76
9b5b61f
3956ebe
4f73db0
255eef0
cd0aba0
7887af8
82d4bb4
52abfa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* _=_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_= | ||
* Repose | ||
* _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- | ||
* Copyright (C) 2010 - 2015 Rackspace US, Inc. | ||
* _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* =_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_=_ | ||
*/ | ||
package org.openrepose.filters.openapivalidator | ||
|
||
import com.atlassian.oai.validator.interaction.request.CustomRequestValidator | ||
import com.atlassian.oai.validator.model.{ApiOperation, Request} | ||
import com.atlassian.oai.validator.report.ValidationReport | ||
import org.openrepose.commons.utils.http.OpenStackServiceHeader.ROLES | ||
import org.openrepose.filters.openapivalidator.RaxRolesValidator.RoleValidationMessageKey | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
class RaxRolesValidator extends CustomRequestValidator { | ||
override def validate(request: Request, apiOperation: ApiOperation): ValidationReport = { | ||
apiOperation.getOperation.getExtensions.asScala.get("x-rax-roles") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to extract the |
||
.map(_.asInstanceOf[java.util.ArrayList[String]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is safe to cast to an |
||
.find(configuredRoles => !request.getHeaderValues(ROLES).asScala.exists(configuredRoles.contains(_))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.map(_ => ValidationReport.singleton(ValidationReport.Message.create(RoleValidationMessageKey, "None of the configured roles match the request.").build())) | ||
.getOrElse(ValidationReport.empty()) | ||
} | ||
} | ||
|
||
object RaxRolesValidator { | ||
val RoleValidationMessageKey = "rax.roles" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By convention, we normally use |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* _=_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_= | ||
* Repose | ||
* _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- | ||
* Copyright (C) 2010 - 2015 Rackspace US, Inc. | ||
* _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* =_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_=_ | ||
*/ | ||
package org.openrepose.filters.openapivalidator | ||
|
||
import com.atlassian.oai.validator.model.{ApiOperation, ApiPath, NormalisedPath, Request} | ||
import io.swagger.v3.oas.models.{Operation, PathItem} | ||
import org.junit.runner.RunWith | ||
import org.openrepose.commons.utils.http.OpenStackServiceHeader.ROLES | ||
import org.scalatest.junit.JUnitRunner | ||
import org.scalatest.mock.MockitoSugar | ||
import org.scalatest.{FunSpec, Matchers} | ||
import org.springframework.mock.web.MockHttpServletRequest | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
class RaxRolesValidatorTest | ||
extends FunSpec with Matchers with MockitoSugar { | ||
|
||
val validator = new RaxRolesValidator | ||
|
||
describe("validate") { | ||
it("should match when there's a singular user role and singular configured role; one of which matches") { | ||
val report = validator.validate(request("banana"), apiOperation("banana")) | ||
report.hasErrors shouldBe false | ||
} | ||
|
||
it("should match when there's a singular user role and multiple configured roles; one of which matches") { | ||
val report = validator.validate(request("banana"), apiOperation("banana", "phone")) | ||
report.hasErrors shouldBe false | ||
} | ||
|
||
it("should match when there are multiple user roles and singular configured role; one of which matches") { | ||
val report = validator.validate(request("banana", "phone"), apiOperation("banana")) | ||
report.hasErrors shouldBe false | ||
} | ||
|
||
it("should match when there are multiple user roles and multiple configured roles; one of which matches") { | ||
val report = validator.validate(request("banana", "phone"), apiOperation("banana", "time")) | ||
report.hasErrors shouldBe false | ||
} | ||
|
||
it("should match when there are multiple user roles and multiple configured roles; several match") { | ||
val report = validator.validate(request("banana", "phone"), apiOperation("banana", "phone", "time")) | ||
report.hasErrors shouldBe false | ||
} | ||
|
||
it("shouldn't match when there are multiple user roles and multiple configured roles; none match") { | ||
val report = validator.validate(request("banana", "phone"), apiOperation("adventure", "time")) | ||
val messages = report.getMessages | ||
messages.size() shouldBe 1 | ||
messages.get(0).getKey shouldBe RaxRolesValidator.RoleValidationMessageKey | ||
} | ||
} | ||
|
||
def request(roles: String*): Request = { | ||
val request = new MockHttpServletRequest() | ||
roles.foreach(request.addHeader(ROLES, _)) | ||
new HttpServletValidatorRequest(request) | ||
} | ||
|
||
def apiOperation(roles: String*): ApiOperation = { | ||
val operation = new Operation() | ||
operation.addExtension("x-rax-roles", roles.toList.asJava) | ||
new ApiOperation(mock[ApiPath], mock[NormalisedPath], PathItem.HttpMethod.GET, operation) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a space before the
->
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about it a little more, I think we are supposed to add a
WWW-Authenticate
header to the response when we set the status to401
. That would probably mean adding another case to the validation report match: