-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwsIamApi.scala
119 lines (106 loc) · 2.78 KB
/
AwsIamApi.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.encalmo.aws
import software.amazon.awssdk.services.iam.model.*
import ujson.{Arr, Str}
import upickle.default.*
import upickle.default.ReadWriter.join
import scala.jdk.CollectionConverters.*
object AwsIamApi {
inline def getRole(roleName: String)(using AwsClient): Role = {
summon[AwsClient].iam
.getRole(GetRoleRequest.builder().roleName(roleName).build())
.role()
}
inline def listRoleAttachedPolicies(
roleName: String
)(using AwsClient): Seq[AttachedPolicy] = {
summon[AwsClient].iam
.listAttachedRolePolicies(
ListAttachedRolePoliciesRequest
.builder()
.roleName(roleName)
.build()
)
.attachedPolicies()
.asScala
.toSeq
}
inline def listRoleInlinedPolicies(
roleName: String
)(using AwsClient): Seq[String] = {
summon[AwsClient].iam
.listRolePolicies(
ListRolePoliciesRequest
.builder()
.roleName(roleName)
.build()
)
.policyNames()
.asScala
.toSeq
}
inline def getRolePolicy(roleName: String, policyName: String)(using
AwsClient
): String = {
summon[AwsClient].iam
.getRolePolicy(
GetRolePolicyRequest
.builder()
.roleName(roleName)
.policyName(policyName)
.build()
)
.policyDocument()
}
inline def getPolicy(policyArn: String)(using AwsClient): Policy = {
summon[AwsClient].iam
.getPolicy(
GetPolicyRequest
.builder()
.policyArn(policyArn: String)
.build()
)
.policy()
}
inline def getPolicyVersionDocument(policyArn: String, versionId: String)(using
AwsClient
): String = {
summon[AwsClient].iam
.getPolicyVersion(
GetPolicyVersionRequest
.builder()
.policyArn(policyArn: String)
.versionId(versionId: String)
.build()
)
.policyVersion()
.document()
}
implicit val stringOrStringArrayReadWrite: ReadWriter[Seq[String]] =
readwriter[ujson.Value].bimap(
{ case ss: Seq[String] =>
if (ss.isEmpty) then ujson.Null
else if (ss.size == 1) then ujson.Str(ss.head)
else ujson.Arr(ss.map(ujson.Str(_)))
},
{
case ujson.Str(s) => Seq(s)
case Arr(a) =>
a.map {
case Str(s) => s
case other =>
throw new Exception(
s"Expected string item in the arrays but got $other"
)
}.toSeq
case other =>
throw new Exception(
s"Expected string or array of strings but got $other"
)
}
)
case class PolicyStatement(
Effect: String,
Action: Seq[String],
Resource: Seq[String]
) derives ReadWriter
}