Skip to content

Commit

Permalink
APIS-3756 Code Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Walker committed Sep 18, 2018
1 parent 39cd143 commit 69853a5
Show file tree
Hide file tree
Showing 110 changed files with 15,561 additions and 5 deletions.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
Third Party Application
=============

# third-party-application
The Third Party Application microservice is responsible for maintaining the state of applications created
by (or on behalf of) third parties to consume APIs registered on the API Platform.

[ ![Download](https://api.bintray.com/packages/hmrc/releases/third-party-application/images/download.svg) ](https://bintray.com/hmrc/releases/third-party-application/_latestVersion)
# Tests
Some tests require `MongoDB` to run.
Thus, remember to start up MongoDB if you want to run the tests locally.
The tests include unit tests and integration tests.
In order to run them, use this command line:

This is a placeholder README.md for a new repository
```
./run_all_tests.sh
```

### License
A report will also be generated identifying any dependencies that need upgrading. This requires that
you have defined CATALOGUE_DEPENDENCIES_URL as an environment variable pointing to the dependencies
endpoint on the Tax Platform Catalogue's API.

This code is open source software licensed under the [Apache 2.0 License]("http://www.apache.org/licenses/LICENSE-2.0.html").
#Current Known Issues
In some use cases, specifically if this microservice is running locally there may be a problem with Wso2.
This can be resolved be entering application.conf and changing:

```
Dev.skipWso2 to true.
```
33 changes: 33 additions & 0 deletions app/Module.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* 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.
*/

import com.google.inject.AbstractModule
import com.typesafe.config.{Config, ConfigFactory}
import play.api.{Configuration, Environment}
import uk.gov.hmrc.config.MicroserviceAuditConnector
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.services.{RealWSO2APIStore, StubAPIStore, WSO2APIStore}

class Module(environment: Environment, configuration: Configuration) extends AbstractModule {

override def configure = {
bind(classOf[Config]).toInstance(ConfigFactory.load())
bind(classOf[AuditConnector]).toInstance(MicroserviceAuditConnector)
val skipWso2 = configuration.getBoolean("skipWso2").getOrElse(false)
if (skipWso2) bind(classOf[WSO2APIStore]).toInstance(StubAPIStore)
else bind(classOf[WSO2APIStore]).to(classOf[RealWSO2APIStore])
}
}
74 changes: 74 additions & 0 deletions app/uk/gov/hmrc/config/AppContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* 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 uk.gov.hmrc.config

import java.util.concurrent.TimeUnit._
import javax.inject.Inject

import com.typesafe.config.Config
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import uk.gov.hmrc.models.ApplicationData
import uk.gov.hmrc.play.config.ServicesConfig
import uk.gov.hmrc.scheduled.JobConfig

import scala.concurrent.duration.{Duration, FiniteDuration}

class AppContext @Inject()(val config: Config) extends ServicesConfig {

lazy val devHubBaseUrl = getConfig(s"$env.devHubBaseUrl")
lazy val skipWso2: Boolean = getConfig(s"$env.skipWso2", runModeConfiguration.getBoolean)
lazy val clientSecretLimit: Int = getConfig(s"clientSecretLimit", runModeConfiguration.getInt)
lazy val trustedApplications: Seq[String] = getConfig(s"$env.trustedApplications", runModeConfiguration.getStringSeq)

lazy val upliftVerificationValidity: FiniteDuration = config.as[Option[FiniteDuration]]("upliftVerificationValidity")
.getOrElse(Duration(90, DAYS)) // scalastyle:off magic.number
lazy val upliftVerificationExpiryJobConfig = config.as[Option[JobConfig]](s"$env.upliftVerificationExpiryJob")
.getOrElse(JobConfig(FiniteDuration(60, SECONDS), FiniteDuration(24, HOURS), enabled = true)) // scalastyle:off magic.number

lazy val refreshSubscriptionsJobConfig = config.as[Option[JobConfig]](s"$env.refreshSubscriptionsJob")
.getOrElse(JobConfig(FiniteDuration(120, SECONDS), FiniteDuration(60, DAYS), enabled = true)) // scalastyle:off magic.number

lazy val devHubTitle: String = "Developer Hub"

lazy val fetchApplicationTtlInSecs : Int = getConfig("fetchApplicationTtlInSeconds", runModeConfiguration.getInt)
lazy val fetchSubscriptionTtlInSecs : Int = getConfig("fetchSubscriptionTtlInSeconds", runModeConfiguration.getInt)

lazy val publishApiDefinition = runModeConfiguration.getBoolean("publishApiDefinition").getOrElse(false)
lazy val apiContext = runModeConfiguration.getString("api.context").getOrElse("third-party-application")
lazy val access = runModeConfiguration.getConfig(s"api.access")

override def toString() = {
"AppContext{" + (
Seq(s"environment=$env",s"skipWso2=$skipWso2",
s"clientSecretLimit=$clientSecretLimit",
s"upliftVerificationValidity=$upliftVerificationValidity",
s"upliftVerificationExpiryJobConfig=$upliftVerificationExpiryJobConfig",
s"trustedApplications=$trustedApplications",
s"fetchApplicationTtlInSecs=$fetchApplicationTtlInSecs",
s"fetchSubscriptionTtlInSecs=$fetchSubscriptionTtlInSecs"
) mkString ",") + "}"
}

private def getConfig(key: String) = runModeConfiguration.getString(key)
.getOrElse(throw new RuntimeException(s"[$key] is not configured!"))

private def getConfig[T](key: String, block: String => Option[T]) = block(key)
.getOrElse(throw new RuntimeException(s"[$key] is not configured!"))

def isTrusted(application: ApplicationData): Boolean = trustedApplications.contains(application.id.toString)
}
108 changes: 108 additions & 0 deletions app/uk/gov/hmrc/config/ApplicationGlobal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* 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 uk.gov.hmrc.config

import com.typesafe.config.Config
import play.api._
import play.api.mvc.EssentialFilter
import uk.gov.hmrc.api.config.{ServiceLocatorConfig, ServiceLocatorRegistration}
import uk.gov.hmrc.api.connector.ServiceLocatorConnector
import uk.gov.hmrc.http._
import uk.gov.hmrc.http.hooks.HttpHooks
import uk.gov.hmrc.play.audit.http.HttpAuditing
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.play.config.{AppName, ControllerConfig, RunMode}
import uk.gov.hmrc.play.http.ws._
import uk.gov.hmrc.play.microservice.bootstrap.DefaultMicroserviceGlobal
import uk.gov.hmrc.play.microservice.config.LoadAuditingConfig
import uk.gov.hmrc.play.microservice.filters._
import uk.gov.hmrc.play.scheduling.{ExclusiveScheduledJob, RunningOfScheduledJobs, ScheduledJob}
import uk.gov.hmrc.scheduled.{RefreshSubscriptionsScheduledJob, UpliftVerificationExpiryJob}

object ApplicationGlobal extends DefaultMicroserviceGlobal with RunMode with RunningOfScheduledJobs
with ServiceLocatorRegistration with ServiceLocatorConfig {

lazy val injector = Play.current.injector
lazy val appContext = injector.instanceOf[AppContext]


override def loggingFilter: LoggingFilter = MicroserviceLoggingFilter

override def microserviceAuditFilter: AuditFilter = MicroserviceAuditFilter

override protected def defaultMicroserviceFilters: Seq[EssentialFilter] = Seq(
Some(metricsFilter),
Some(microserviceAuditFilter),
Some(loggingFilter),
Some(DefaultToNoCacheFilter),
Some(RecoveryFilter)).flatten

override val hc = HeaderCarrier()
override val slConnector = ServiceLocatorConnector(WSHttp)
override lazy val registrationEnabled = appContext.publishApiDefinition

override def authFilter = None

override def auditConnector: AuditConnector = MicroserviceAuditConnector

override def microserviceMetricsConfig(implicit app: Application): Option[Configuration] =
app.configuration.getConfig(s"$env.microservice.metrics")

override lazy val scheduledJobs: Seq[ScheduledJob] = {
val upliftJob: Seq[ExclusiveScheduledJob] = if (appContext.upliftVerificationExpiryJobConfig.enabled) {
Seq(injector.instanceOf[UpliftVerificationExpiryJob])
} else {
Seq.empty
}

val refreshJob = if (appContext.refreshSubscriptionsJobConfig.enabled) {
Seq(injector.instanceOf[RefreshSubscriptionsScheduledJob])
} else {
Seq.empty
}
upliftJob ++ refreshJob
}
}

object ControllerConfiguration extends ControllerConfig {

import net.ceedubs.ficus.Ficus._

lazy val controllerConfigs = Play.current.configuration.underlying.as[Config]("controllers")
}

object MicroserviceAuditFilter extends AuditFilter with AppName with MicroserviceFilterSupport {
override val auditConnector = MicroserviceAuditConnector

override def controllerNeedsAuditing(controllerName: String) = ControllerConfiguration.paramsForController(controllerName).needsAuditing
}

object MicroserviceLoggingFilter extends LoggingFilter with MicroserviceFilterSupport {
override def controllerNeedsLogging(controllerName: String) = ControllerConfiguration.paramsForController(controllerName).needsLogging
}

object MicroserviceAuditConnector extends AuditConnector with RunMode {
override lazy val auditingConfig = LoadAuditingConfig(s"$env.auditing")
}

trait Hooks extends HttpHooks with HttpAuditing {
override val hooks = Seq(AuditingHook)
override lazy val auditConnector: AuditConnector = MicroserviceAuditConnector
}

trait WSHttp extends HttpGet with WSGet with HttpPut with WSPut with HttpPost with WSPost with HttpDelete with WSDelete with Hooks with AppName
object WSHttp extends WSHttp
39 changes: 39 additions & 0 deletions app/uk/gov/hmrc/connector/APIDefinitionConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* 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 uk.gov.hmrc.connector

import java.util.UUID

import javax.inject.Inject
import uk.gov.hmrc.config.WSHttp
import uk.gov.hmrc.http.{HeaderCarrier, HttpReads}
import uk.gov.hmrc.models.APIDefinition

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ExecutionContext, Future}

class APIDefinitionConnector @Inject() extends HttpConnector {
lazy val serviceUrl = baseUrl("api-definition")
val http = WSHttp

def fetchAllAPIs(applicationId: UUID)(implicit rds: HttpReads[Seq[APIDefinition]], hc: HeaderCarrier, ec: ExecutionContext): Future[Seq[APIDefinition]] = {
val url = s"$serviceUrl/api-definition?applicationId=$applicationId"
http.GET[Seq[APIDefinition]](url).map(result => result) recover {
case e => throw new RuntimeException(s"Unexpected response from $url: ${e.getMessage}")
}
}
}
38 changes: 38 additions & 0 deletions app/uk/gov/hmrc/connector/ApiSubscriptionFieldsConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* 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 uk.gov.hmrc.connector

import javax.inject.Inject

import uk.gov.hmrc.config.WSHttp
import uk.gov.hmrc.http.{HeaderCarrier, NotFoundException}
import uk.gov.hmrc.models.HasSucceeded

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class ApiSubscriptionFieldsConnector @Inject() extends HttpConnector {

lazy val serviceUrl = baseUrl("api-subscription-fields")
val http = WSHttp

def deleteSubscriptions(clientId: String)(implicit hc: HeaderCarrier): Future[HasSucceeded] = {
http.DELETE(s"$serviceUrl/field/application/$clientId") map (_ => HasSucceeded) recover {
case _: NotFoundException => HasSucceeded
}
}
}
44 changes: 44 additions & 0 deletions app/uk/gov/hmrc/connector/AuthConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* 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 uk.gov.hmrc.connector

import javax.inject.Inject

import uk.gov.hmrc.config.WSHttp
import uk.gov.hmrc.http.{HeaderCarrier, Upstream4xxResponse}
import uk.gov.hmrc.models.AuthRole

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class AuthConnector @Inject() extends HttpConnector {

val authUrl: String = s"${baseUrl("auth")}/auth/authenticate/user"
val http = WSHttp

def authorized(role: AuthRole)(implicit hc: HeaderCarrier): Future[Boolean] = authorized(role.scope, Some(role.name))

def authorized(scope: String, role: Option[String])(implicit hc: HeaderCarrier): Future[Boolean] = {
val authoriseUrl =
role.map(aRole => s"$authUrl/authorise?scope=$scope&role=$aRole")
.getOrElse(s"$authUrl/authorise?scope=$scope")

http.GET(authoriseUrl) map (_ => true) recover {
case e: Upstream4xxResponse if e.upstreamResponseCode == 401 => false
}
}
}
Loading

0 comments on commit 69853a5

Please sign in to comment.