github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/okta/factor.go (about)

     1  // Copyright 2021 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package okta
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/okta/okta-sdk-golang/v2/okta"
    22  	"github.com/okta/terraform-provider-okta/sdk"
    23  )
    24  
    25  type FactorGenerator struct {
    26  	OktaService
    27  }
    28  
    29  func (g FactorGenerator) createResources(ctx context.Context, factorList []*okta.UserFactor, client *sdk.APISupplement) []terraformutils.Resource {
    30  	var resources []terraformutils.Resource
    31  	for _, factor := range factorList {
    32  		if factor.Status == "ACTIVE" {
    33  			resources = append(resources, terraformutils.NewResource(
    34  				factor.Id,
    35  				"factor_"+normalizeResourceNameWithRandom(factor.Id, true),
    36  				"okta_factor",
    37  				"okta",
    38  				map[string]string{
    39  					"provider_id": factor.Id,
    40  				},
    41  				[]string{},
    42  				map[string]interface{}{},
    43  			))
    44  
    45  			if factor.FactorType == "token:hotp" {
    46  				hotpFactorProfiles, _, _ := getHotpFactorProfiles(ctx, client)
    47  
    48  				for _, factorProfile := range hotpFactorProfiles {
    49  					if factorProfile != nil {
    50  						resources = append(resources, terraformutils.NewResource(
    51  							factorProfile.ID,
    52  							"factor_totp_"+normalizeResourceNameWithRandom(factorProfile.Name, true),
    53  							"okta_factor_totp",
    54  							"okta",
    55  							map[string]string{},
    56  							[]string{},
    57  							map[string]interface{}{
    58  								"name":                   factorProfile.Name,
    59  								"otp_length":             factorProfile.Settings.OtpLength,
    60  								"time_step":              factorProfile.Settings.TimeStep,
    61  								"clock_drift_interval":   factorProfile.Settings.AcceptableAdjacentIntervals,
    62  								"shared_secret_encoding": factorProfile.Settings.Encoding,
    63  								"hmac_algorithm":         factorProfile.Settings.TimeStep,
    64  							},
    65  						))
    66  					}
    67  				}
    68  			}
    69  		}
    70  	}
    71  	return resources
    72  }
    73  
    74  func (g *FactorGenerator) InitResources() error {
    75  	var factors []*okta.UserFactor
    76  
    77  	ctx, client, err := g.APISupplementClient()
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	output, _, err := getListFactors(ctx, client)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	factors = append(factors, output...)
    88  
    89  	g.Resources = g.createResources(ctx, factors, client)
    90  	return nil
    91  }
    92  
    93  func getListFactors(ctx context.Context, m *sdk.APISupplement) ([]*okta.UserFactor, *okta.Response, error) {
    94  	//NOTE: Okta SDK does not support general ListFactors method so we got to manually implement the REST calls.
    95  	url := "/api/v1/org/factors"
    96  	req, err := m.RequestExecutor.NewRequest("GET", url, nil)
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  	var factors []*okta.UserFactor
   101  	resp, err := m.RequestExecutor.Do(ctx, req, &factors)
   102  	if err != nil {
   103  		return nil, resp, err
   104  	}
   105  	return factors, resp, nil
   106  }
   107  
   108  func getHotpFactorProfiles(ctx context.Context, m *sdk.APISupplement) ([]*sdk.HotpFactorProfile, *okta.Response, error) {
   109  	url := "/api/v1/org/factors/hotp/profiles"
   110  	req, err := m.RequestExecutor.NewRequest("GET", url, nil)
   111  	if err != nil {
   112  		return nil, nil, err
   113  	}
   114  	var factors []*sdk.HotpFactorProfile
   115  	resp, err := m.RequestExecutor.Do(ctx, req, &factors)
   116  	if err != nil {
   117  		return nil, resp, err
   118  	}
   119  	return factors, resp, nil
   120  }