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

353 - improve credentials settings #354

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/main/scala/zio/s3/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import software.amazon.awssdk.services.s3.S3AsyncClient
import software.amazon.awssdk.services.s3.model.S3Exception
import zio.nio.file.{ Path => ZPath }
import zio.s3.S3Bucket.S3BucketListing
import zio.s3.providers.const
import zio.s3.providers.{ basic, const }
import zio.stream.ZStream

import java.net.URI
Expand All @@ -32,7 +32,7 @@ package object s3 {
type S3Stream[A] = ZStream[S3, S3Exception, A]

def live(region: Region, credentials: AwsCredentials, uriEndpoint: Option[URI] = None): Layer[S3Exception, S3] =
liveZIO(region, const(credentials.accessKeyId, credentials.secretAccessKey), uriEndpoint)
liveZIO(region, const(credentials), uriEndpoint)

def liveZIO[R](
region: Region,
Expand All @@ -50,7 +50,7 @@ package object s3 {

val live: ZLayer[S3Settings, ConnectionError, S3] = ZLayer.scoped(
ZIO.serviceWithZIO[S3Settings](s =>
Live.connect(s.s3Region, const(s.credentials.accessKeyId, s.credentials.secretAccessKey), None)
Live.connect(s.s3Region, basic(s.credentials.accessKeyId, s.credentials.secretAccessKey), None)
)
)

Expand Down
10 changes: 8 additions & 2 deletions src/main/scala/zio/s3/providers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import zio.{ IO, Scope, UIO, ZIO }

object providers {

def const(accessKeyId: String, secretAccessKey: String): UIO[AwsCredentialsProvider] =
ZIO.succeedNow[AwsCredentialsProvider](() => AwsBasicCredentials.create(accessKeyId, secretAccessKey))
def const(credential: AwsCredentials): UIO[AwsCredentialsProvider] =
ZIO.succeedNow[AwsCredentialsProvider](() => credential)

def basic(accessKeyId: String, secretAccessKey: String): UIO[AwsCredentialsProvider] =
const(AwsBasicCredentials.create(accessKeyId, secretAccessKey))

def session(accessKeyId: String, secretAccessKey: String, sessionToken: String): UIO[AwsCredentialsProvider] =
const(AwsSessionCredentials.create(accessKeyId, secretAccessKey, sessionToken))

val system: IO[InvalidCredentials, SystemPropertyCredentialsProvider] =
ZIO
Expand Down
15 changes: 10 additions & 5 deletions src/test/scala/zio/s3/S3ProvidersTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio.s3

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.{ AwsBasicCredentials, AwsSessionCredentials }
import software.amazon.awssdk.regions.Region
import zio.s3.providers._
import zio.test.Assertion._
Expand All @@ -25,14 +25,19 @@ object S3ProvidersTest extends ZIOSpecDefault {

def spec: Spec[TestEnvironment with Scope, Any] =
suite("Providers")(
test("cred with const") {
test("basic credentials") {
ZIO
.scoped(const("k", "v").map(_.resolveCredentials()))
.scoped(basic("k", "v").map(_.resolveCredentials()))
.map(res => assertTrue(res == AwsBasicCredentials.create("k", "v")))
},
test("cred with default fallback const") {
test("session credentials") {
ZIO
.scoped((env <> const("k", "v")).map(_.resolveCredentials()))
.scoped(session("k", "v", "t").map(_.resolveCredentials()))
.map(res => assertTrue(res == AwsSessionCredentials.create("k", "v", "t")))
},
test("basic credentials default fallback const") {
ZIO
.scoped((env <> basic("k", "v")).map(_.resolveCredentials()))
.map(res => assertTrue(res == AwsBasicCredentials.create("k", "v")))
},
test("cred in system properties") {
Expand Down