-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ID-1301 talk to ecm for nih username (Round 2) (#1407)
- Loading branch information
Showing
28 changed files
with
862 additions
and
85 deletions.
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
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
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
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
32 changes: 32 additions & 0 deletions
32
src/main/scala/org/broadinstitute/dsde/firecloud/dataaccess/DisabledExternalCredsDAO.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,32 @@ | ||
package org.broadinstitute.dsde.firecloud.dataaccess | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import org.broadinstitute.dsde.firecloud.model.{LinkedEraAccount, UserInfo, WithAccessToken} | ||
|
||
import scala.concurrent.Future | ||
|
||
class DisabledExternalCredsDAO extends ExternalCredsDAO with LazyLogging { | ||
|
||
override def getLinkedAccount(implicit userInfo: UserInfo): Future[Option[LinkedEraAccount]] = Future.successful { | ||
logger.warn("Getting Linked eRA Account from ECM, but ECM is disabled.") | ||
None | ||
} | ||
|
||
override def putLinkedEraAccount(linkedEraAccount: LinkedEraAccount)(implicit orchInfo: WithAccessToken): Future[Unit] = Future.successful { | ||
logger.warn("Putting Linked eRA Account to ECM, but ECM is disabled.") | ||
} | ||
|
||
override def deleteLinkedEraAccount(userInfo: UserInfo)(implicit orchInfo: WithAccessToken): Future[Unit] = Future.successful { | ||
logger.warn("Deleting Linked eRA Account from ECM, but ECM is disabled.") | ||
} | ||
|
||
override def getLinkedEraAccountForUsername(username: String)(implicit orchInfo: WithAccessToken): Future[Option[LinkedEraAccount]] = Future.successful { | ||
logger.warn("Getting Linked eRA Account for username from ECM, but ECM is disabled.") | ||
None | ||
} | ||
|
||
override def getActiveLinkedEraAccounts(implicit orchInfo: WithAccessToken): Future[Seq[LinkedEraAccount]] = Future.successful { | ||
logger.warn("Getting Active Linked eRA Accounts from ECM, but ECM is disabled.") | ||
Seq.empty | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/scala/org/broadinstitute/dsde/firecloud/dataaccess/ExternalCredsDAO.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,25 @@ | ||
package org.broadinstitute.dsde.firecloud.dataaccess | ||
|
||
import org.broadinstitute.dsde.firecloud.model.{LinkedEraAccount, UserInfo, WithAccessToken} | ||
import org.databiosphere.workspacedata.client.ApiException | ||
|
||
import scala.concurrent.Future | ||
|
||
trait ExternalCredsDAO { | ||
|
||
@throws(classOf[ApiException]) | ||
def getLinkedAccount(implicit userInfo: UserInfo): Future[Option[LinkedEraAccount]] | ||
|
||
@throws(classOf[ApiException]) | ||
def putLinkedEraAccount(linkedEraAccount: LinkedEraAccount)(implicit orchInfo: WithAccessToken): Future[Unit] | ||
|
||
@throws(classOf[ApiException]) | ||
def deleteLinkedEraAccount(userInfo: UserInfo)(implicit orchInfo: WithAccessToken): Future[Unit] | ||
|
||
@throws(classOf[ApiException]) | ||
def getLinkedEraAccountForUsername(username: String)(implicit orchInfo: WithAccessToken): Future[Option[LinkedEraAccount]] | ||
|
||
@throws(classOf[ApiException]) | ||
def getActiveLinkedEraAccounts(implicit orchInfo: WithAccessToken): Future[Seq[LinkedEraAccount]] | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
src/main/scala/org/broadinstitute/dsde/firecloud/dataaccess/HttpExternalCredsDAO.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,86 @@ | ||
package org.broadinstitute.dsde.firecloud.dataaccess | ||
|
||
import bio.terra.externalcreds.api.OauthApi | ||
import bio.terra.externalcreds.api.AdminApi | ||
import bio.terra.externalcreds.client.ApiClient | ||
import bio.terra.externalcreds.model.Provider | ||
import com.google.api.client.http.HttpStatusCodes | ||
import org.broadinstitute.dsde.firecloud.FireCloudConfig | ||
import org.broadinstitute.dsde.firecloud.model.LinkedEraAccount.unapply | ||
import org.broadinstitute.dsde.firecloud.model.{LinkedEraAccount, UserInfo, WithAccessToken} | ||
import org.broadinstitute.dsde.workbench.model.WorkbenchException | ||
import org.joda.time.DateTime | ||
import org.springframework.web.client.{HttpClientErrorException, RestTemplate} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.jdk.CollectionConverters._ | ||
|
||
class HttpExternalCredsDAO(implicit val executionContext: ExecutionContext) extends ExternalCredsDAO { | ||
|
||
private lazy val restTemplate = new RestTemplate | ||
|
||
private def handleError[A](e: HttpClientErrorException, operation: String): Option[A] = { | ||
e.getStatusCode.value() match { | ||
case HttpStatusCodes.STATUS_CODE_NOT_FOUND => None | ||
case _ => throw new WorkbenchException(s"Failed to $operation: ${e.getMessage}") | ||
} | ||
} | ||
|
||
override def getLinkedAccount(implicit userInfo: UserInfo): Future[Option[LinkedEraAccount]] = Future { | ||
val oauthApi: OauthApi = getOauthApi(userInfo.accessToken.token) | ||
try { | ||
val linkInfo = oauthApi.getLink(Provider.ERA_COMMONS) | ||
Some(LinkedEraAccount(userInfo.id, linkInfo.getExternalUserId, new DateTime(linkInfo.getExpirationTimestamp))) | ||
} catch { | ||
case e: HttpClientErrorException => handleError(e, "GET eRA Linked Account") | ||
} | ||
} | ||
|
||
override def putLinkedEraAccount(linkedEraAccount: LinkedEraAccount)(implicit orchInfo: WithAccessToken): Future[Unit] = Future { | ||
val adminApi = getAdminApi(orchInfo.accessToken.token) | ||
adminApi.putLinkedAccountWithFakeToken(unapply(linkedEraAccount), Provider.ERA_COMMONS) | ||
} | ||
|
||
override def deleteLinkedEraAccount(userInfo: UserInfo)(implicit orchInfo: WithAccessToken): Future[Unit] = Future { | ||
val adminApi = getAdminApi(orchInfo.accessToken.token) | ||
try { | ||
adminApi.adminDeleteLinkedAccount(userInfo.id, Provider.ERA_COMMONS) | ||
} catch { | ||
case e: HttpClientErrorException => handleError(e, "DELETE eRA Linked Account") | ||
} | ||
} | ||
|
||
override def getLinkedEraAccountForUsername(username: String)(implicit orchInfo: WithAccessToken): Future[Option[LinkedEraAccount]] = Future { | ||
val adminApi = getAdminApi(orchInfo.accessToken.token) | ||
try { | ||
val adminLinkInfo = adminApi.getLinkedAccountForExternalId(Provider.ERA_COMMONS, username) | ||
Some(LinkedEraAccount(adminLinkInfo)) | ||
} catch { | ||
case e: HttpClientErrorException => handleError(e, s"GET eRA Linked Account for username [$username]") | ||
} | ||
} | ||
|
||
override def getActiveLinkedEraAccounts(implicit orchInfo: WithAccessToken): Future[Seq[LinkedEraAccount]] = Future { | ||
val adminApi = getAdminApi(orchInfo.accessToken.token) | ||
val adminLinkInfos = adminApi.getActiveLinkedAccounts(Provider.ERA_COMMONS) | ||
adminLinkInfos.asScala.map(LinkedEraAccount.apply).toSeq | ||
} | ||
|
||
private def getApi(accessToken: String): ApiClient = { | ||
val client = new ApiClient(restTemplate) | ||
client.setBasePath(FireCloudConfig.ExternalCreds.baseUrl) | ||
client.setAccessToken(accessToken) | ||
client | ||
} | ||
|
||
private def getOauthApi(accessToken: String): OauthApi = { | ||
val client = getApi(accessToken) | ||
new OauthApi(client) | ||
} | ||
|
||
private def getAdminApi(accessToken: String): AdminApi = { | ||
val client = getApi(accessToken) | ||
new AdminApi(client) | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/scala/org/broadinstitute/dsde/firecloud/model/LinkedEraAccount.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,25 @@ | ||
package org.broadinstitute.dsde.firecloud.model | ||
|
||
import bio.terra.externalcreds.model.AdminLinkInfo | ||
import org.joda.time.{DateTime, Instant} | ||
|
||
object LinkedEraAccount { | ||
def apply(samUserId: String, nihLink: NihLink): LinkedEraAccount = { | ||
LinkedEraAccount(samUserId, nihLink.linkedNihUsername, Instant.ofEpochSecond(nihLink.linkExpireTime).toDateTime) | ||
} | ||
|
||
def apply(adminLinkInfo: AdminLinkInfo): LinkedEraAccount = { | ||
LinkedEraAccount(adminLinkInfo.getUserId, adminLinkInfo.getLinkedExternalId, new DateTime(adminLinkInfo.getLinkExpireTime)) | ||
} | ||
|
||
def unapply(linkedEraAccount: LinkedEraAccount): AdminLinkInfo = { | ||
new AdminLinkInfo() | ||
.userId(linkedEraAccount.userId) | ||
.linkedExternalId(linkedEraAccount.linkedExternalId) | ||
.linkExpireTime(linkedEraAccount.linkExpireTime.toDate) | ||
} | ||
} | ||
|
||
case class LinkedEraAccount(userId: String, linkedExternalId: String, linkExpireTime: DateTime) | ||
|
||
|
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
Oops, something went wrong.