-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_kafka_topic_rbac.go
179 lines (156 loc) · 4.83 KB
/
resource_kafka_topic_rbac.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cplatform
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
confluent "github.com/OneMount/gonfluent"
)
var err error
// roleBindings will bind the roles to users/scope
// example:
// resource "kafka_topic_rbac" "user_confluent_test" {
// principal = "User:confluent-test"
// role = "Operation"
// cluster_id = "zxvdlaskdjal"
// cluster_type = "Kafka"
//
// provider = confluent-kafka.confluent
// }
// Resource ID = principal + "|" + clusterId + "|" + principal + "|" + role
func kafkaTopicRBAC() *schema.Resource {
return &schema.Resource{
CreateContext: kafkaTopicRBACCreate,
DeleteContext: kafkaTopicRBACDelete,
ReadContext: kafkaTopicRBACRead,
Schema: map[string]*schema.Schema{
"principal": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "Defined the principal - User or subject",
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if !strings.Contains(v, "User:") || !strings.Contains(v, "Group:") && strings.Contains(v, "|") {
errs = append(errs, fmt.Errorf("%q must be defined with User: or Group: and must not have |, got: %s", key, v))
}
return
},
},
"role": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "Role of",
DefaultFunc: schema.EnvDefaultFunc("ROLE", "DeveloperRead"),
ValidateFunc: validation.StringInSlice(scopeRole, false),
},
"resource_type": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("RESOURCE_TYPE", ""),
},
"pattern_type": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validation.StringInSlice(validPatternType, false),
},
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"cluster_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
Description: "The ID of cluster",
},
},
}
}
func kafkaTopicRBACRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*confluent.Client)
principal := d.Get("principal").(string)
role := d.Get("role").(string)
clusterId := d.Get("cluster_id").(string)
cDetails := &confluent.ClusterDetails{}
cDetails.Clusters.KafkaCluster = clusterId
roleBindings, err := c.LookupRoleBinding(principal, role, *cDetails)
if err != nil {
log.Printf("[ERROR] Error lookup role-binding %s from Confluent", err)
return diag.FromErr(err)
}
r := make(map[string]string)
for _, v := range roleBindings {
if v.Name == d.Get("name").(string) {
r[v.ResourceType] = v.PatternType
}
}
if r[d.Get("resource_type").(string)] != d.Get("pattern_type").(string) {
err = fmt.Errorf("cannot find resource_type" + d.Get("resource_type").(string) + " of" + d.Get("name").(string))
log.Printf("[ERROR] Error lookup role-binding from Confluent %s", err)
return diag.FromErr(err)
}
return nil
}
func kafkaTopicRBACCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*confluent.Client)
principal := d.Get("principal").(string)
role := d.Get("role").(string)
clusterId:= d.Get("cluster_id").(string)
cDetails := &confluent.ClusterDetails{
Clusters: confluent.Clusters{
KafkaCluster: clusterId,
},
}
u := confluent.RoleBinding{
Scope: *cDetails,
ResourcePatterns: []confluent.ResourcePattern{
{
ResourceType: d.Get("resource_type").(string),
Name: d.Get("name").(string),
PatternType: d.Get("pattern_type").(string),
},
},
}
err = c.IncreaseRoleBinding(principal, role, u)
if err != nil {
return diag.FromErr(err)
}
rId := clusterId + "|" + principal + "|" + role + "|" + d.Get("resource_type").(string) + "|" + d.Get("name").(string) + "|" + d.Get("pattern_type").(string)
d.SetId(rId)
return nil
}
func kafkaTopicRBACDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*confluent.Client)
principal := d.Get("principal").(string)
role := d.Get("role").(string)
clusterId:= d.Get("cluster_id").(string)
cDetails := &confluent.ClusterDetails{
Clusters: confluent.Clusters{
KafkaCluster: clusterId,
},
}
u := confluent.RoleBinding{
Scope: *cDetails,
ResourcePatterns: []confluent.ResourcePattern{
{
ResourceType: d.Get("resource_type").(string),
Name: d.Get("name").(string),
PatternType: d.Get("pattern_type").(string),
},
},
}
err = c.DecreaseRoleBinding(principal, role, u)
if err != nil {
return diag.FromErr(err)
}
return nil
}