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

REP-7789 Added Rax Roles extension to OpenApi #2070

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class OpenApiValidatorFilter @Inject()(@Value(ReposeSpringProperties.CORE.CONFIG

validator = OpenApiInteractionValidator
.createFor(resolveHref(newConfiguration.getHref))
.withCustomRequestValidation(new RaxRolesValidator())
.build()

Thread.currentThread.setContextClassLoader(contextClassLoader)
Expand Down Expand Up @@ -172,6 +173,7 @@ object OpenApiValidatorFilter {
"validation.request.accept.invalid" -> HttpServletResponse.SC_BAD_REQUEST,
"validation.request.accept.notAllowed" -> HttpServletResponse.SC_NOT_ACCEPTABLE,
"validation.schema.invalidJson" -> HttpServletResponse.SC_BAD_REQUEST,
RaxRolesValidator.RoleValidationMessageKey-> HttpServletResponse.SC_UNAUTHORIZED,
Copy link
Contributor

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 ->.

Copy link
Contributor

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 to 401. That would probably mean adding another case to the validation report match:

case Some(ValidationFailure(HttpServletResponse.SC_UNAUTHORIZED, message)) =>
            logger.debug("blahblahblah")
            httpResponse.addHeader("WWW-Authenticate", "blah blah")
            httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, message)

"validation.schema.unknownError" -> HttpServletResponse.SC_INTERNAL_SERVER_ERROR
)

Expand Down
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")
Copy link
Contributor

Choose a reason for hiding this comment

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

It might make sense to extract the x-rax-roles string into a constant.

.map(_.asInstanceOf[java.util.ArrayList[String]])
Copy link
Contributor

Choose a reason for hiding this comment

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

It is safe to cast to an ArrayList? Would casting to a List be safer? Is throwing an exception here acceptable if the user provided something other than a list as the value to our extension (in configuration)? Ideally, I guess, we would validate the document itself on configuration update, but that is a new challenge.

.find(configuredRoles => !request.getHeaderValues(ROLES).asScala.exists(configuredRoles.contains(_)))
Copy link
Contributor

Choose a reason for hiding this comment

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

configuredRoles.contains(_) can be simplified to configuredRoles.contains (dropping the parameter list with wildcard).

.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"
Copy link
Contributor

Choose a reason for hiding this comment

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

By convention, we normally use final val to signify constants, right?

}
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)
}
}
2 changes: 1 addition & 1 deletion versions.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
com.atlassian.oai:swagger-request-validator-core=2.2.2
com.atlassian.oai:swagger-request-validator-core=2.3.0
jacksonVersion=2.9.9
com.fasterxml.jackson.core:jackson-annotations=$jacksonVersion
com.fasterxml.jackson.core:jackson-core=$jacksonVersion
Expand Down