/// <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();
        }
Exemple #2
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;
         }
     }));
 }