Skip to content

Commit

Permalink
add support for clients generating API tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
bpholt committed Oct 26, 2023
1 parent 7a70e90 commit 30de0cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.holt.imds

import cats.effect.*
import cats.effect.std.*
import cats.syntax.all.*
import io.circe.literal.*
import io.circe.syntax.*
Expand All @@ -10,7 +11,7 @@ import org.http4s.circe.jsonEncoder
import org.http4s.dsl.Http4sDsl
import org.http4s.server.Router

class FakeInstanceMetadataService[F[_] : Concurrent](instanceMetadata: InstanceMetadata[F]) extends Http4sDsl[F] {
class FakeInstanceMetadataService[F[_] : Concurrent : UUIDGen](instanceMetadata: InstanceMetadata[F]) extends Http4sDsl[F] {

private val iam: HttpRoutes[F] = Router("iam" -> HttpRoutes.of[F] {
case GET -> Root / "security-credentials" | GET -> Root / "security-credentials" / "" =>
Expand Down Expand Up @@ -82,9 +83,19 @@ class FakeInstanceMetadataService[F[_] : Concurrent](instanceMetadata: InstanceM

}

private val api = HttpRoutes.of[F] {
/* TODO this should actually record the generated token with a passed TTL, and
then validate that incoming requests include the token, but it's not clear
to me why this matters in this text context, so I'm punting on it for now
*/
case req@PUT -> Root / "token" =>
req.as[Unit] *> UUIDGen.randomString.flatMap(Ok(_))
}

val routes: HttpRoutes[F] = Router(
"/latest/meta-data" -> latestMetaData,
"/latest/dynamic" -> dynamic,
"/latest/api" -> api
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.holt.imds
import cats.Applicative
import cats.data.NonEmptyList
import cats.effect.*
import cats.effect.std.Env
import cats.effect.std.{Env, UUIDGen}
import cats.laws.discipline.arbitrary.*
import cats.syntax.all.*
import com.comcast.ip4s.*
Expand All @@ -27,6 +27,7 @@ import org.scalacheck.{Arbitrary, Gen, Shrink}
import java.time.Instant
import java.time.format.DateTimeFormatter.ISO_INSTANT
import java.time.temporal.ChronoUnit.HOURS
import java.util.UUID
import scala.collection.immutable
import scala.concurrent.duration.FiniteDuration

Expand Down Expand Up @@ -208,6 +209,19 @@ class FakeInstanceMetadataServiceSpec
}
}

test("PUT /latest/api/token") {
forAllF { (generatedToken: UUID) =>
implicit val uuidGen: UUIDGen[IO] = new UUIDGen[IO] {
override def randomUUID: IO[UUID] = generatedToken.pure[IO]
}

val client = org.http4s.client.Client.fromHttpApp(new FakeInstanceMetadataService(InstanceMetadata[IO]).routes.orNotFound)

client.expect[String](PUT(uri"/latest/api/token"))
.map(assertEquals(_, generatedToken.toString))
}
}

test("GET /latest/dynamic/instance-identity/document") {
forAllF { (defaultRegion: Option[String],
region: Option[String],
Expand Down

0 comments on commit 30de0cf

Please sign in to comment.