Example #1
0
        private static int ListServiceAccounts(ListOptions opts)
        {
            var response = ServiceAccountServiceClient.ListServiceAccounts(new ListServiceAccountsRequest
            {
                ProjectName = opts.ProjectName,
            });

            foreach (var serviceAccount in response)
            {
                Console.WriteLine("-----------------------------");
                Console.WriteLine($"Name: {serviceAccount.Name}");
                Console.WriteLine($"ID: {serviceAccount.Id}");
                Console.WriteLine($"Creation time : {serviceAccount.CreationTime}");

                var daysUntilExpiry =
                    Math.Floor((serviceAccount.ExpirationTime.ToDateTime() - DateTime.UtcNow).TotalDays);
                Console.WriteLine(daysUntilExpiry < 0
                    ? $"Expired {Math.Abs(daysUntilExpiry)} day(s) ago"
                    : $"Expiring in {daysUntilExpiry} day(s)");

                Console.WriteLine($"Permissions: {serviceAccount.Permissions}");
                Console.WriteLine("-----------------------------");
            }
            return(0);
        }
Example #2
0
 public async Task <object> GetServiceAccountByName(dynamic data)
 {
     return(await Task.Run(() => {
         PlatformRefreshTokenCredential CredentialWithProvidedToken = new PlatformRefreshTokenCredential(data._RefreshToken);
         ServiceAccountServiceClient _serviceAccountServiceClient = ServiceAccountServiceClient.Create(credentials: CredentialWithProvidedToken);
         var _account = _serviceAccountServiceClient.ListServiceAccounts(new ListServiceAccountsRequest
         {
             ProjectName = data._ProjectName
         }).Where(account => account.Name == data._ServiceAccountName);
         if (_account.Count() > 0)
         {
             return _account.First().Id.ToString();
         }
         else
         {
             return null;
         }
     }));
 }
Example #3
0
        /// <summary>
        ///     This contains the implementation of the "Service account maintenance" scenario.
        ///     1. Iterate over the service accounts in your project.
        ///     2. If a service account has expired, or is close to expiry, prolong the expiry time to some point in the
        ///     future.
        /// </summary>
        /// <param name="args"></param>
        private static void Main(string[] args)
        {
            // Set up some new service accounts
            Setup();

            Console.WriteLine("Getting the service accounts that you have permission to view...");

            var serviceAccounts = ServiceAccountServiceClient.ListServiceAccounts(new ListServiceAccountsRequest
            {
                ProjectName = ProjectName
            })
                                  .Where(serviceAccount => (serviceAccount.ExpirationTime.ToDateTime() - DateTime.UtcNow).TotalDays <=
                                         DaysRemainingAtWhichExpiryShouldBeIncreased);

            foreach (var serviceAccount in serviceAccounts)
            {
                // Calculate how many days it is until the service account expires, and output a message
                // depending on whether it has already expired, or is close to expiry

                var daysUntilExpiry =
                    Math.Floor((serviceAccount.ExpirationTime.ToDateTime() - DateTime.UtcNow).TotalDays);

                Console.WriteLine(daysUntilExpiry < 0
                    ? $"Service account '{serviceAccount.Name}' expired {Math.Abs(daysUntilExpiry)} day(s) ago"
                    : $"Service account '{serviceAccount.Name}' will expire in {daysUntilExpiry} day(s)");

                // Now extend the lifetime by increasing the expiry time relative to the current time

                Console.WriteLine(
                    $"Extending service account '{serviceAccount.Name}' expiry time by {DaysToExpandServiceAccountBy} days from now");

                ServiceAccountServiceClient.UpdateServiceAccount(new UpdateServiceAccountRequest
                {
                    Id             = serviceAccount.Id,
                    ExpirationTime = DateTime.UtcNow.AddDays(DaysToExpandServiceAccountBy).ToTimestamp()
                });
            }

            Console.WriteLine("No more service accounts found");

            Cleanup();
        }
Example #4
0
 public async Task <object> IncreaseServiceAccountLifetime(dynamic data)
 {
     return(await Task.Run(() => {
         PlatformRefreshTokenCredential CredentialWithProvidedToken = new PlatformRefreshTokenCredential(data._RefreshToken);
         ServiceAccountServiceClient _serviceAccountServiceClient = ServiceAccountServiceClient.Create(credentials: CredentialWithProvidedToken);
         var _account = _serviceAccountServiceClient.ListServiceAccounts(new ListServiceAccountsRequest
         {
             ProjectName = data._ProjectName
         }).Where(account => account.Id == data._ServiceAccountID);
         if (_account.Count() > 0)
         {
             _serviceAccountServiceClient.UpdateServiceAccount(new UpdateServiceAccountRequest
             {
                 Id = _account.First().Id,
                 ExpirationTime = DateTime.UtcNow.AddSeconds(data._Lifeitime).ToTimestamp()
             });
             return _account.First().Id.ToString();
         }
         else
         {
             return null;
         }
     }));
 }