Skip to content

Commit

Permalink
Add Application, Role, Group, RoleAssignment support
Browse files Browse the repository at this point in the history
  • Loading branch information
vaspahomov committed Sep 29, 2021
1 parent 1c07021 commit e109a80
Show file tree
Hide file tree
Showing 24 changed files with 2,841 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apis/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
databasev1alpha3 "github.com/crossplane/provider-azure/apis/database/v1alpha3"
databasev1beta1 "github.com/crossplane/provider-azure/apis/database/v1beta1"
networkv1alpha3 "github.com/crossplane/provider-azure/apis/network/v1alpha3"
rbacv1alpha1 "github.com/crossplane/provider-azure/apis/rbac/v1alpha1"
storagev1alpha3 "github.com/crossplane/provider-azure/apis/storage/v1alpha3"
azurev1alpha3 "github.com/crossplane/provider-azure/apis/v1alpha3"
azurev1beta1 "github.com/crossplane/provider-azure/apis/v1beta1"
Expand All @@ -40,6 +41,7 @@ func init() {
databasev1alpha3.SchemeBuilder.AddToScheme,
databasev1beta1.SchemeBuilder.AddToScheme,
networkv1alpha3.SchemeBuilder.AddToScheme,
rbacv1alpha1.SchemeBuilder.AddToScheme,
storagev1alpha3.SchemeBuilder.AddToScheme,
)
}
Expand Down
18 changes: 18 additions & 0 deletions apis/rbac/rbac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2021 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package rbac contains Azure rbac API versions
package rbac
21 changes: 21 additions & 0 deletions apis/rbac/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 contains managed resources for Azure rbac.
// +kubebuilder:object:generate=true
// +groupName=rbac.azure.crossplane.io
// +versionName=v1alpha1
package v1alpha1
127 changes: 127 additions & 0 deletions apis/rbac/v1alpha1/referencers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"

"github.com/pkg/errors"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crossplane/crossplane-runtime/pkg/reference"
"github.com/crossplane/crossplane-runtime/pkg/resource"
)

// RoleID - reference to id of role
func RoleID() reference.ExtractValueFn {
return func(mg resource.Managed) string {
s, ok := mg.(*Role)
if !ok {
return ""
}
return s.Spec.RoleID
}
}

// ADGroupID - reference to id of ad group
func ADGroupID() reference.ExtractValueFn {
return func(mg resource.Managed) string {
s, ok := mg.(*ADGroup)
if !ok {
return ""
}
return s.Spec.GroupID
}
}

// ResolveReferences of this ServicePrincipal
func (mg *ServicePrincipal) ResolveReferences(ctx context.Context, c client.Reader) error {
r := reference.NewAPIResolver(c, mg)

// Resolve spec.applicationID
rsp, err := r.Resolve(ctx, reference.ResolutionRequest{
CurrentValue: mg.Spec.ApplicationID,
Reference: mg.Spec.ApplicationIDRef,
Selector: mg.Spec.ApplicationIDSelector,
To: reference.To{Managed: &Application{}, List: &ApplicationList{}},
Extract: reference.ExternalName(),
})
if err != nil {
return errors.Wrap(err, "spec.applicationID")
}
mg.Spec.ApplicationID = rsp.ResolvedValue
mg.Spec.ApplicationIDRef = rsp.ResolvedReference

return nil
}

// ResolveReferences of this ServicePrincipal
func (mg *RoleAssignment) ResolveReferences(ctx context.Context, c client.Reader) error {
r := reference.NewAPIResolver(c, mg)

switch mg.Spec.PrincipalType {
case "":
fallthrough
case "service-principal":
// Resolve spec.principalID
rsp, err := r.Resolve(ctx, reference.ResolutionRequest{
CurrentValue: mg.Spec.PrincipalID,
Reference: mg.Spec.GroupIDRef,
Selector: mg.Spec.GroupIDSelector,
To: reference.To{Managed: &ServicePrincipal{}, List: &ServicePrincipalList{}},
Extract: reference.ExternalName(),
})
if err != nil {
return errors.Wrap(err, "spec.principalID")
}
mg.Spec.PrincipalID = rsp.ResolvedValue
mg.Spec.PrincipalIDRef = rsp.ResolvedReference
case "group":
// Resolve spec.principalID
rsp, err := r.Resolve(ctx, reference.ResolutionRequest{
CurrentValue: mg.Spec.PrincipalID,
Reference: mg.Spec.GroupIDRef,
Selector: mg.Spec.GroupIDSelector,
To: reference.To{Managed: &ADGroup{}, List: &ADGroupList{}},
Extract: ADGroupID(),
})
if err != nil {
return errors.Wrap(err, "spec.principalID")
}
mg.Spec.PrincipalID = rsp.ResolvedValue
mg.Spec.GroupIDRef = rsp.ResolvedReference
default:
return errors.New("invalid spec.principalType should be one of: ['group', 'service-principal']")
}

// Resolve spec.roleID
rsp, err := r.Resolve(ctx, reference.ResolutionRequest{
CurrentValue: mg.Spec.RoleID,
Reference: mg.Spec.RoleIDRef,
Selector: mg.Spec.RoleIDSelector,
To: reference.To{Managed: &Role{}, List: &RoleList{}},
Extract: RoleID(),
})
if err != nil {
return errors.Wrap(err, "spec.roleID")
}
mg.Spec.RoleID = rsp.ResolvedValue
mg.Spec.RoleIDRef = rsp.ResolvedReference

return nil
}
74 changes: 74 additions & 0 deletions apis/rbac/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"reflect"

"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

// Package type metadata.
const (
Group = "rbac.azure.crossplane.io"
Version = "v1alpha1"
)

var (
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: Group, Version: Version}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
)

// Application type metadata.
var (
ApplicationKind = reflect.TypeOf(Application{}).Name()
ApplicationGroupKind = schema.GroupKind{Group: Group, Kind: ApplicationKind}.String()
ApplicationKindAPIVersion = ApplicationKind + "." + SchemeGroupVersion.String()
ApplicationGroupVersionKind = SchemeGroupVersion.WithKind(ApplicationKind)

ServicePrincipalKind = reflect.TypeOf(ServicePrincipal{}).Name()
ServicePrincipalGroupKind = schema.GroupKind{Group: Group, Kind: ServicePrincipalKind}.String()
ServicePrincipalKindAPIVersion = ServicePrincipalKind + "." + SchemeGroupVersion.String()
ServicePrincipalGroupVersionKind = SchemeGroupVersion.WithKind(ServicePrincipalKind)

RoleAssignmentKind = reflect.TypeOf(RoleAssignment{}).Name()
RoleAssignmentGroupKind = schema.GroupKind{Group: Group, Kind: RoleAssignmentKind}.String()
RoleAssignmentKindAPIVersion = RoleAssignmentKind + "." + SchemeGroupVersion.String()
RoleAssignmentGroupVersionKind = SchemeGroupVersion.WithKind(RoleAssignmentKind)

RoleKind = reflect.TypeOf(Role{}).Name()
RoleGroupKind = schema.GroupKind{Group: Group, Kind: RoleKind}.String()
RoleKindAPIVersion = RoleKind + "." + SchemeGroupVersion.String()
RoleGroupVersionKind = SchemeGroupVersion.WithKind(RoleKind)

ADGroupKind = reflect.TypeOf(ADGroup{}).Name()
ADGroupGroupKind = schema.GroupKind{Group: Group, Kind: ADGroupKind}.String()
ADGroupKindAPIVersion = ADGroupKind + "." + SchemeGroupVersion.String()
ADGroupGroupVersionKind = SchemeGroupVersion.WithKind(ADGroupKind)
)

func init() {
SchemeBuilder.Register(&Application{}, &ApplicationList{})
SchemeBuilder.Register(&ServicePrincipal{}, &ServicePrincipalList{})
SchemeBuilder.Register(&RoleAssignment{}, &RoleAssignmentList{})
SchemeBuilder.Register(&Role{}, &RoleList{})
SchemeBuilder.Register(&ADGroup{}, &ADGroupList{})
}
Loading

0 comments on commit e109a80

Please sign in to comment.