|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +angular.module("openshiftConsole") |
| 4 | + .factory("SecurityCheckService", function(APIService, $filter, Constants) { |
| 5 | + var humanizeKind = $filter('humanizeKind'); |
| 6 | + var getSecurityAlerts = function(resources, project) { |
| 7 | + var alerts = []; |
| 8 | + var clusterScopedResources = []; |
| 9 | + var roleBindingResources = []; |
| 10 | + var roleResources = []; |
| 11 | + var notWhitelistedResources = []; |
| 12 | + _.each(resources, function(resource) { |
| 13 | + if (!_.get(resource, "kind")) { |
| 14 | + // This isn't a valid API object |
| 15 | + return; |
| 16 | + } |
| 17 | + var rgv = APIService.objectToResourceGroupVersion(resource); |
| 18 | + var apiInfo = APIService.apiInfo(rgv); |
| 19 | + if (!apiInfo.namespaced) { |
| 20 | + clusterScopedResources.push(resource); |
| 21 | + } |
| 22 | + else if (rgv.resource === "rolebindings" && (rgv.group === '' || rgv.group === "rbac.authorization.k8s.io")) { |
| 23 | + // If role in the rolebinding is one of the "safe" ones ignore it (view or image puller), otherwise warn |
| 24 | + var roleRef = _.get(resource, 'roleRef.name'); |
| 25 | + if (roleRef !== 'view' && roleRef !== 'system:image-puller') { |
| 26 | + roleBindingResources.push(resource); |
| 27 | + } |
| 28 | + } |
| 29 | + else if (rgv.resource === "roles" && (rgv.group === '' || rgv.group === "rbac.authorization.k8s.io")) { |
| 30 | + roleResources.push(resource); |
| 31 | + } |
| 32 | + else if (!_.find(Constants.SECURITY_CHECK_WHITELIST, {resource: rgv.resource, group: rgv.group})) { |
| 33 | + notWhitelistedResources.push(resource); |
| 34 | + } |
| 35 | + }); |
| 36 | + if (clusterScopedResources.length) { |
| 37 | + var clusterStrs = _.uniq(_.map(clusterScopedResources, function(resource) { |
| 38 | + return humanizeKind(resource.kind); |
| 39 | + })); |
| 40 | + alerts.push({ |
| 41 | + type: 'warning', |
| 42 | + message: "This will create resources outside of the project, which might impact all users of the cluster.", |
| 43 | + details: "Typically only cluster administrators can create these resources. The cluster-level resources being created are: " + clusterStrs.join(", ") |
| 44 | + }); |
| 45 | + } |
| 46 | + if (roleBindingResources.length) { |
| 47 | + var roleBindingStrs = []; |
| 48 | + _.each(roleBindingResources, function(resource){ |
| 49 | + _.each(resource.subjects, function(subject) { |
| 50 | + var str = humanizeKind(subject.kind) + " "; |
| 51 | + if (subject.kind === 'ServiceAccount') { |
| 52 | + str += (subject.namespace || project) + "/"; |
| 53 | + } |
| 54 | + str += subject.name; |
| 55 | + roleBindingStrs.push(str); |
| 56 | + }); |
| 57 | + }); |
| 58 | + roleBindingStrs = _.uniq(roleBindingStrs); |
| 59 | + alerts.push({ |
| 60 | + type: 'warning', |
| 61 | + message: "This will grant permissions to your project.", |
| 62 | + details: "Permissions are being granted to: " + roleBindingStrs.join(", ") |
| 63 | + }); |
| 64 | + } |
| 65 | + if (roleResources.length) { |
| 66 | + alerts.push({ |
| 67 | + type: 'info', |
| 68 | + message: "This will create additional membership roles within the project.", |
| 69 | + details: "Admins will be able to grant these custom roles to users, groups, and service accounts." |
| 70 | + }); |
| 71 | + } |
| 72 | + if (notWhitelistedResources.length) { |
| 73 | + var notWhitelistStrs = _.uniq(_.map(notWhitelistedResources, function(resource) { |
| 74 | + return humanizeKind(resource.kind); |
| 75 | + })); |
| 76 | + alerts.push({ |
| 77 | + type: 'warning', |
| 78 | + message: "This will create resources that may have security or project behavior implications.", |
| 79 | + details: "Make sure you understand what they do before creating them. The resources being created are: " + notWhitelistStrs.join(", ") |
| 80 | + }); |
| 81 | + } |
| 82 | + return alerts; |
| 83 | + }; |
| 84 | + |
| 85 | + return { |
| 86 | + // Gets security alerts relevant to a set of resources |
| 87 | + // Returns: Array of alerts |
| 88 | + getSecurityAlerts: getSecurityAlerts |
| 89 | + }; |
| 90 | + }); |
0 commit comments