forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for the permissions instead of using a CLI flag. open-telemetry…
…#2588 Signed-off-by: Israel Blancas <iblancasa@gmail.com>
- Loading branch information
Showing
5 changed files
with
204 additions
and
63 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
.chloggen/replace-create-rbac-permissions-by-checking-the-sa-permissions.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action) | ||
component: operator | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Check the permissions for the SA running the OpenTelemetry Operator instead of using the create-rbac-permissions flag. | ||
|
||
# One or more tracking issues related to the change | ||
issues: [2588] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright The OpenTelemetry 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 autodetect | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
func GetOperatorNamespace() (string, error) { | ||
nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(nsBytes), nil | ||
} | ||
|
||
func GetOperatorServiceAccount() (string, error) { | ||
restConfig, err := rest.InClusterConfig() | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
namespace, err := GetOperatorNamespace() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
deploymentName := "opentelemetry-operator" | ||
|
||
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{ | ||
LabelSelector: fmt.Sprintf("app.kubernetes.io/name=%s", deploymentName), | ||
}) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return pods.Items[0].Spec.ServiceAccountName, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright The OpenTelemetry 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 | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
v1 "k8s.io/api/authorization/v1" | ||
) | ||
|
||
// WarningsGroupedByResource is a helper to take the missing permissions and format them as warnings. | ||
func WarningsGroupedByResource(reviews []*v1.SubjectAccessReview) []string { | ||
fullResourceToVerbs := make(map[string][]string) | ||
for _, review := range reviews { | ||
if review.Spec.ResourceAttributes != nil { | ||
key := fmt.Sprintf("%s/%s", review.Spec.ResourceAttributes.Group, review.Spec.ResourceAttributes.Resource) | ||
if len(review.Spec.ResourceAttributes.Group) == 0 { | ||
key = review.Spec.ResourceAttributes.Resource | ||
} | ||
fullResourceToVerbs[key] = append(fullResourceToVerbs[key], review.Spec.ResourceAttributes.Verb) | ||
} else if review.Spec.NonResourceAttributes != nil { | ||
key := fmt.Sprintf("nonResourceURL: %s", review.Spec.NonResourceAttributes.Path) | ||
fullResourceToVerbs[key] = append(fullResourceToVerbs[key], review.Spec.NonResourceAttributes.Verb) | ||
} | ||
} | ||
var warnings []string | ||
for fullResource, verbs := range fullResourceToVerbs { | ||
warnings = append(warnings, fmt.Sprintf("missing the following rules for %s: [%s]", fullResource, strings.Join(verbs, ","))) | ||
} | ||
return warnings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters