-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[New Scheduler]Implement PFCInvokerServer #5098
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
core/invoker/src/main/scala/org/apache/openwhisk/core/invoker/DefaultInvokerServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.openwhisk.core.invoker | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.model.headers.BasicHttpCredentials | ||
import akka.http.scaladsl.server.Route | ||
import org.apache.openwhisk.common.{Logging, TransactionId} | ||
import org.apache.openwhisk.http.BasicRasService | ||
import org.apache.openwhisk.http.ErrorResponse.terminate | ||
import spray.json.PrettyPrinter | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
/** | ||
* Implements web server to handle certain REST API calls. | ||
*/ | ||
class DefaultInvokerServer(val invoker: InvokerCore, systemUsername: String, systemPassword: String)( | ||
implicit val ec: ExecutionContext, | ||
val actorSystem: ActorSystem, | ||
val logger: Logging) | ||
extends BasicRasService { | ||
|
||
/** Pretty print JSON response. */ | ||
implicit val jsonPrettyResponsePrinter = PrettyPrinter | ||
|
||
override def routes(implicit transid: TransactionId): Route = { | ||
super.routes ~ extractCredentials { | ||
case Some(BasicHttpCredentials(username, password)) if username == systemUsername && password == systemPassword => | ||
(path("enable") & post) { | ||
invoker.enable() | ||
} ~ (path("disable") & post) { | ||
invoker.disable() | ||
} | ||
case _ => terminate(StatusCodes.Unauthorized) | ||
} | ||
} | ||
} | ||
|
||
object DefaultInvokerServer extends InvokerServerProvider { | ||
|
||
// TODO: TBD, after FPCInvokerReactive is ready, can read the credentials from pureconfig | ||
val invokerUsername = "admin" | ||
val invokerPassword = "admin" | ||
|
||
override def instance( | ||
invoker: InvokerCore)(implicit ec: ExecutionContext, actorSystem: ActorSystem, logger: Logging): BasicRasService = | ||
new DefaultInvokerServer(invoker, invokerUsername, invokerPassword) | ||
} |
65 changes: 65 additions & 0 deletions
65
core/invoker/src/main/scala/org/apache/openwhisk/core/invoker/FPCInvokerServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.openwhisk.core.invoker | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.model.headers.BasicHttpCredentials | ||
import akka.http.scaladsl.server.Route | ||
import org.apache.openwhisk.common.{Logging, TransactionId} | ||
import org.apache.openwhisk.http.BasicRasService | ||
import org.apache.openwhisk.http.ErrorResponse.terminate | ||
import spray.json.PrettyPrinter | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
/** | ||
* Implements web server to handle certain REST API calls. | ||
*/ | ||
class FPCInvokerServer(val invoker: InvokerCore, systemUsername: String, systemPassword: String)( | ||
implicit val ec: ExecutionContext, | ||
val actorSystem: ActorSystem, | ||
val logger: Logging) | ||
extends BasicRasService { | ||
|
||
/** Pretty print JSON response. */ | ||
implicit val jsonPrettyResponsePrinter = PrettyPrinter | ||
|
||
override def routes(implicit transid: TransactionId): Route = { | ||
super.routes ~ extractCredentials { | ||
case Some(BasicHttpCredentials(username, password)) if username == systemUsername && password == systemPassword => | ||
(path("enable") & post) { | ||
invoker.enable() | ||
} ~ (path("disable") & post) { | ||
invoker.disable() | ||
} | ||
case _ => terminate(StatusCodes.Unauthorized) | ||
} | ||
} | ||
} | ||
|
||
object FPCInvokerServer extends InvokerServerProvider { | ||
|
||
// TODO: TBD, after FPCInvokerReactive is ready, can read the credentials from pureconfig | ||
val invokerUsername = "admin" | ||
val invokerPassword = "admin" | ||
|
||
override def instance( | ||
invoker: InvokerCore)(implicit ec: ExecutionContext, actorSystem: ActorSystem, logger: Logging): BasicRasService = | ||
new FPCInvokerServer(invoker, invokerUsername, invokerPassword) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package org.apache.openwhisk.core.invoker | |
|
||
import akka.Done | ||
import akka.actor.{ActorSystem, CoordinatedShutdown} | ||
import akka.http.scaladsl.server.Route | ||
import akka.stream.ActorMaterializer | ||
import com.typesafe.config.ConfigValueFactory | ||
import kamon.Kamon | ||
|
@@ -217,7 +218,10 @@ trait InvokerProvider extends Spi { | |
} | ||
|
||
// this trait can be used to add common implementation | ||
trait InvokerCore {} | ||
trait InvokerCore { | ||
def enable(): Route | ||
def disable(): Route | ||
} | ||
|
||
/** | ||
* An Spi for providing RestAPI implementation for invoker. | ||
|
@@ -227,9 +231,3 @@ trait InvokerServerProvider extends Spi { | |
def instance( | ||
invoker: InvokerCore)(implicit ec: ExecutionContext, actorSystem: ActorSystem, logger: Logging): BasicRasService | ||
} | ||
|
||
object DefaultInvokerServer extends InvokerServerProvider { | ||
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. I moved DefaultInvokerServer to a separate file, keep the same as FPCInvokerServer. |
||
override def instance( | ||
invoker: InvokerCore)(implicit ec: ExecutionContext, actorSystem: ActorSystem, logger: Logging): BasicRasService = | ||
new BasicRasService {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
tests/src/test/scala/org/apache/openwhisk/core/invoker/test/DefaultInvokerServerTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.openwhisk.core.invoker.test | ||
|
||
import akka.http.scaladsl.model.StatusCodes.{OK, Unauthorized} | ||
import akka.http.scaladsl.model.headers.BasicHttpCredentials | ||
import akka.http.scaladsl.server.Route | ||
import akka.http.scaladsl.testkit.ScalatestRouteTest | ||
import common.StreamLogging | ||
import org.apache.openwhisk.common.TransactionId | ||
import org.apache.openwhisk.core.invoker.{DefaultInvokerServer, InvokerCore} | ||
import org.apache.openwhisk.http.BasicHttpService | ||
import org.junit.runner.RunWith | ||
import org.scalamock.scalatest.MockFactory | ||
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FlatSpec, Matchers} | ||
import org.scalatest.junit.JUnitRunner | ||
|
||
/** | ||
* Tests InvokerServer API. | ||
*/ | ||
@RunWith(classOf[JUnitRunner]) | ||
class DefaultInvokerServerTests | ||
extends FlatSpec | ||
with BeforeAndAfterEach | ||
with BeforeAndAfterAll | ||
with ScalatestRouteTest | ||
with Matchers | ||
with StreamLogging | ||
with MockFactory { | ||
|
||
def transid() = TransactionId("tid") | ||
|
||
val systemUsername = "username" | ||
val systemPassword = "password" | ||
|
||
val reactive = new TestInvokerReactive | ||
val server = new DefaultInvokerServer(reactive, systemUsername, systemPassword) | ||
|
||
override protected def afterEach(): Unit = reactive.reset() | ||
|
||
/** DefaultInvokerServer API tests */ | ||
behavior of "DefaultInvokerServer API" | ||
|
||
it should "enable invoker" in { | ||
implicit val tid = transid() | ||
val validCredentials = BasicHttpCredentials(systemUsername, systemPassword) | ||
Post(s"/enable") ~> addCredentials(validCredentials) ~> Route.seal(server.routes(tid)) ~> check { | ||
status should be(OK) | ||
reactive.enableCount shouldBe 1 | ||
reactive.disableCount shouldBe 0 | ||
} | ||
} | ||
|
||
it should "disable invoker" in { | ||
implicit val tid = transid() | ||
val validCredentials = BasicHttpCredentials(systemUsername, systemPassword) | ||
Post(s"/disable") ~> addCredentials(validCredentials) ~> Route.seal(server.routes(tid)) ~> check { | ||
status should be(OK) | ||
reactive.enableCount shouldBe 0 | ||
reactive.disableCount shouldBe 1 | ||
} | ||
} | ||
|
||
it should "not enable invoker with invalid credential" in { | ||
implicit val tid = transid() | ||
val invalidCredentials = BasicHttpCredentials("invaliduser", "invalidpass") | ||
Post(s"/enable") ~> addCredentials(invalidCredentials) ~> Route.seal(server.routes(tid)) ~> check { | ||
status should be(Unauthorized) | ||
reactive.enableCount shouldBe 0 | ||
reactive.disableCount shouldBe 0 | ||
} | ||
} | ||
|
||
it should "not disable invoker with invalid credential" in { | ||
implicit val tid = transid() | ||
val invalidCredentials = BasicHttpCredentials("invaliduser", "invalidpass") | ||
Post(s"/disable") ~> addCredentials(invalidCredentials) ~> Route.seal(server.routes(tid)) ~> check { | ||
status should be(Unauthorized) | ||
reactive.enableCount shouldBe 0 | ||
reactive.disableCount shouldBe 0 | ||
} | ||
} | ||
|
||
it should "not enable invoker with empty credential" in { | ||
implicit val tid = transid() | ||
Post(s"/enable") ~> Route.seal(server.routes(tid)) ~> check { | ||
status should be(Unauthorized) | ||
reactive.enableCount shouldBe 0 | ||
reactive.disableCount shouldBe 0 | ||
} | ||
} | ||
|
||
it should "not disable invoker with empty credential" in { | ||
implicit val tid = transid() | ||
Post(s"/disable") ~> Route.seal(server.routes(tid)) ~> check { | ||
status should be(Unauthorized) | ||
reactive.enableCount shouldBe 0 | ||
reactive.disableCount shouldBe 0 | ||
} | ||
} | ||
|
||
} | ||
|
||
class TestInvokerReactive extends InvokerCore with BasicHttpService { | ||
var enableCount = 0 | ||
var disableCount = 0 | ||
|
||
override def enable(): Route = { | ||
enableCount += 1 | ||
complete("") | ||
} | ||
|
||
override def disable(): Route = { | ||
disableCount += 1 | ||
complete("") | ||
} | ||
|
||
def reset(): Unit = { | ||
enableCount = 0 | ||
disableCount = 0 | ||
} | ||
|
||
/** | ||
* Gets the routes implemented by the HTTP service. | ||
* | ||
* @param transid the id for the transaction (every request is assigned an id) | ||
*/ | ||
override def routes(implicit transid: TransactionId): Route = ??? | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not a problem for this mr, but I don't think that's a secure way of setting admin credentials
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.
Yes, after FPCInvokerReactive is ready, can read the credentials from pureconfig like other components.