Example #1
0
        public PSApiKey(ApplicationInsightsComponentApiKey key)
        {
            this.ApiKey      = key.ApiKey;
            this.CreatedDate = key.CreatedDate;
            this.Id          = key.Id.Split('/')[10];
            this.Description = key.Name;

            List <string> accessPermission = new List <string>();

            ApiKeyRole[] roles = new ApiKeyRole[] {
                ReadTelemetry,
                WriteAnnotations,
                ReadAgentConfiguration
            };

            if (key.LinkedReadProperty != null)
            {
                foreach (var readaccess in key.LinkedReadProperty)
                {
                    var role = ExtraRole(roles, readaccess);
                    if (role != null)
                    {
                        accessPermission.Add(role.roleName);
                    }
                }
            }

            if (key.LinkedWriteProperty != null)
            {
                foreach (var writeAccess in key.LinkedWriteProperty)
                {
                    var role = ExtraRole(roles, writeAccess);
                    if (role != null)
                    {
                        accessPermission.Add(role.roleName);
                    }
                }
            }

            this.Permissions = accessPermission.ToArray();
        }
Example #2
0
        internal static Tuple <string[], string[]> BuildApiKeyAccess(string subscriptionId, string resourceGroup, string componentName, string[] permissions)
        {
            ApiKeyRole[] roles = new ApiKeyRole[] {
                ReadTelemetry,
                WriteAnnotations,
                ReadAgentConfiguration
            };

            List <string> readAccess  = new List <string>();
            List <string> writeAccess = new List <string>();

            foreach (var permission in permissions)
            {
                var role = roles.FirstOrDefault(r => StringComparer.OrdinalIgnoreCase.Equals(r.roleName, permission));
                if (role != null)
                {
                    if (role.readProperties != null)
                    {
                        foreach (var readProperty in role.readProperties)
                        {
                            readAccess.Add($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/microsoft.insights/components/{componentName}/{readProperty}");
                        }
                    }

                    if (role.writeProperties != null)
                    {
                        foreach (var writeProperty in role.writeProperties)
                        {
                            writeAccess.Add($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/microsoft.insights/components/{componentName}/{writeProperty}");
                        }
                    }
                }
            }

            return(Tuple.Create(readAccess.ToArray(), writeAccess.ToArray()));
        }