github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/ias/fake_client.go (about)

     1  package ias
     2  
     3  import "fmt"
     4  
     5  const (
     6  	FakeIdentityProviderName = "IdentityProviderName"
     7  	FakeIdentityProviderID   = "0dbae593-ab1d-4774-97c1-5118ea22ea2d"
     8  	FakeGrafanaName          = "GrafanaName"
     9  	FakeGrafanaID            = "eebb54dd-e4d5-43a1-929a-e98ea2831342"
    10  	FakeUserForRest          = "874a7fd7-7f0c-482d-ba44-3563b2622586"
    11  	FakeDexName              = "DexName"
    12  	FakeDexID                = "dd70d82e-0a30-4931-9171-3a55a0725512"
    13  	FakeClientID             = "cid"
    14  	FakeClientSecret         = "csc"
    15  )
    16  
    17  type FakeClient struct {
    18  	serviceProviders []*ServiceProvider
    19  }
    20  
    21  func NewFakeClient() *FakeClient {
    22  	return &FakeClient{
    23  		serviceProviders: []*ServiceProvider{
    24  			{
    25  				ID:          FakeGrafanaID,
    26  				DisplayName: fmt.Sprintf("SKR Grafana (instanceID: %s)", FakeGrafanaName),
    27  				AssertionAttributes: []AssertionAttribute{
    28  					{
    29  						AssertionAttribute: "test",
    30  						UserAttribute:      "test",
    31  					},
    32  				},
    33  				UserForRest: FakeUserForRest,
    34  			},
    35  			{
    36  				ID:          FakeDexID,
    37  				DisplayName: fmt.Sprintf("SKR Dex (instanceID: %s)", FakeDexName),
    38  				AssertionAttributes: []AssertionAttribute{
    39  					{
    40  						AssertionAttribute: "test",
    41  						UserAttribute:      "test",
    42  					},
    43  				},
    44  				UserForRest: "2f27c57d-1f05-4c0b-b84a-8fdeeb0de6c0",
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func (f *FakeClient) GetCompany() (*Company, error) {
    51  	var sp []ServiceProvider
    52  	for _, fsp := range f.serviceProviders {
    53  		sp = append(sp, *fsp)
    54  	}
    55  
    56  	return &Company{
    57  		ServiceProviders: sp,
    58  		IdentityProviders: []IdentityProvider{
    59  			{
    60  				Name: FakeIdentityProviderName,
    61  				ID:   FakeIdentityProviderID,
    62  			},
    63  		},
    64  	}, nil
    65  }
    66  
    67  func (f *FakeClient) CreateServiceProvider(name string, _ string) error {
    68  	f.serviceProviders = append(f.serviceProviders, &ServiceProvider{
    69  		DisplayName: name,
    70  	})
    71  
    72  	return nil
    73  }
    74  
    75  func (f *FakeClient) SetDefaultAuthenticatingIDP(config DefaultAuthIDPConfig) error {
    76  	serviceProvider, err := f.GetServiceProvider(config.ID)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	serviceProvider.AuthenticatingIdp.ID = FakeIdentityProviderID
    82  	serviceProvider.AuthenticatingIdp.Name = FakeIdentityProviderName
    83  
    84  	return nil
    85  }
    86  
    87  func (f FakeClient) GenerateServiceProviderSecret(ss SecretConfiguration) (*ServiceProviderSecret, error) {
    88  	serviceProvider, err := f.GetServiceProvider(ss.ID)
    89  	if err != nil {
    90  		return &ServiceProviderSecret{}, err
    91  	}
    92  
    93  	serviceProvider.Secret = append(serviceProvider.Secret, SPSecret{
    94  		SecretID:    FakeClientID,
    95  		Description: ss.RestAPIClientSecret.Description,
    96  		Scopes:      ss.RestAPIClientSecret.Scopes,
    97  	})
    98  
    99  	return &ServiceProviderSecret{
   100  		ClientID:     FakeClientID,
   101  		ClientSecret: FakeClientSecret,
   102  	}, nil
   103  }
   104  
   105  func (f FakeClient) AuthenticationURL(id ProviderID) string {
   106  	return fmt.Sprintf("https://authentication.com/%s", id)
   107  }
   108  
   109  func (f *FakeClient) SetOIDCConfiguration(id string, iasType OIDCType) error {
   110  	serviceProvider, err := f.GetServiceProvider(id)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	serviceProvider.SsoType = iasType.SsoType
   116  	serviceProvider.RedirectURIs = iasType.OpenIDConnectConfig.RedirectURIs
   117  
   118  	return nil
   119  }
   120  
   121  func (f *FakeClient) SetSAMLConfiguration(id string, iasType SAMLType) error {
   122  	serviceProvider, err := f.GetServiceProvider(id)
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	serviceProvider.SsoType = "saml2"
   128  	serviceProvider.ACSEndpoints = iasType.ACSEndpoints
   129  
   130  	return nil
   131  }
   132  
   133  func (f FakeClient) SetAssertionAttribute(id string, paa PostAssertionAttributes) error {
   134  	serviceProvider, err := f.GetServiceProvider(id)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	serviceProvider.AssertionAttributes = paa.AssertionAttributes
   140  
   141  	return nil
   142  }
   143  
   144  func (f FakeClient) SetSubjectNameIdentifier(id string, sni SubjectNameIdentifier) error {
   145  	serviceProvider, err := f.GetServiceProvider(id)
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	serviceProvider.NameIDAttribute = sni.NameIDAttribute
   151  
   152  	return nil
   153  }
   154  
   155  func (f FakeClient) SetAuthenticationAndAccess(id string, auth AuthenticationAndAccess) error {
   156  	serviceProvider, err := f.GetServiceProvider(id)
   157  	if err != nil {
   158  		return err
   159  	}
   160  
   161  	serviceProvider.RBAConfig = auth.ServiceProviderAccess.RBAConfig
   162  
   163  	return nil
   164  }
   165  
   166  func (f *FakeClient) DeleteServiceProvider(id string) error {
   167  	for index, sp := range f.serviceProviders {
   168  		if sp.ID == id {
   169  			f.serviceProviders[index] = f.serviceProviders[len(f.serviceProviders)-1]
   170  			f.serviceProviders[len(f.serviceProviders)-1] = nil
   171  			f.serviceProviders = f.serviceProviders[:len(f.serviceProviders)-1]
   172  			return nil
   173  		}
   174  	}
   175  
   176  	return nil
   177  }
   178  
   179  func (f *FakeClient) DeleteSecret(payload SecretsRef) error {
   180  	for _, provider := range f.serviceProviders {
   181  		if provider.UserForRest != payload.ClientID {
   182  			continue
   183  		}
   184  		for _, scID := range payload.ClientSecretsIDs {
   185  			f.removeSecrets(provider, scID)
   186  		}
   187  	}
   188  
   189  	return nil
   190  }
   191  
   192  func (f *FakeClient) removeSecrets(provider *ServiceProvider, secretID string) {
   193  	var newSecrets []SPSecret
   194  	for _, secret := range provider.Secret {
   195  		if secret.SecretID != secretID {
   196  			newSecrets = append(newSecrets, secret)
   197  		}
   198  	}
   199  
   200  	provider.Secret = newSecrets
   201  }
   202  
   203  func (f *FakeClient) GetServiceProvider(id string) (*ServiceProvider, error) {
   204  	for _, sp := range f.serviceProviders {
   205  		if sp.ID == id {
   206  			return sp, nil
   207  		}
   208  	}
   209  
   210  	return nil, fmt.Errorf("cannot find ServiceProvider with ID: %s", id)
   211  }