From 76ccbdaa9dfbaf5333f12be3a00a04809344ddc0 Mon Sep 17 00:00:00 2001 From: Meet Rathod Date: Sun, 26 Jun 2022 23:03:45 +0530 Subject: [PATCH 1/2] SSH key related models and enum changes --- .../nextgen/enum_ssh_authentication_type.go | 20 +++++++++++++++++++ .../nextgen/enum_ssh_specification_type.go | 20 +++++++++++++++++++ harness/nextgen/model_base_ssh_spec.go | 4 +++- harness/nextgen/model_ssh_auth.go | 2 +- harness/nextgen/model_ssh_credential_spec.go | 5 ++++- harness/nextgen/model_ssh_key_spec.go | 4 ++-- .../nextgen/model_tgt_generation_spec_dto.go | 4 +++- 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 harness/nextgen/enum_ssh_authentication_type.go create mode 100644 harness/nextgen/enum_ssh_specification_type.go diff --git a/harness/nextgen/enum_ssh_authentication_type.go b/harness/nextgen/enum_ssh_authentication_type.go new file mode 100644 index 00000000..4d602bcc --- /dev/null +++ b/harness/nextgen/enum_ssh_authentication_type.go @@ -0,0 +1,20 @@ +package nextgen + +type SSHAuthenticationType string + +var SSHAuthenticationTypes = struct { + Kerberos SSHAuthenticationType + SSH SSHAuthenticationType +}{ + Kerberos: "Kerberos", + SSH: "SSH", +} + +var SSHAuthenticationTypeValues = []string{ + SSHAuthenticationTypes.Kerberos.String(), + SSHAuthenticationTypes.SSH.String(), +} + +func (e SSHAuthenticationType) String() string { + return string(e) +} diff --git a/harness/nextgen/enum_ssh_specification_type.go b/harness/nextgen/enum_ssh_specification_type.go new file mode 100644 index 00000000..463e2905 --- /dev/null +++ b/harness/nextgen/enum_ssh_specification_type.go @@ -0,0 +1,20 @@ +package nextgen + +type SSHSpecificationType string + +var SSHSpecificationTypes = struct { + KerberosConfigDTO SSHSpecificationType + SSHConfig SSHSpecificationType +}{ + KerberosConfigDTO: "KerberosConfigDTO", + SSHConfig: "SSHConfig", +} + +var SSHSpecificationTypeValues = []string{ + SSHSpecificationTypes.KerberosConfigDTO.String(), + SSHSpecificationTypes.SSHConfig.String(), +} + +func (e SSHSpecificationType) String() string { + return string(e) +} diff --git a/harness/nextgen/model_base_ssh_spec.go b/harness/nextgen/model_base_ssh_spec.go index 8b80cf93..504b0951 100644 --- a/harness/nextgen/model_base_ssh_spec.go +++ b/harness/nextgen/model_base_ssh_spec.go @@ -11,5 +11,7 @@ package nextgen // This is the SSH specification details as defined in Harness. type BaseSshSpec struct { - Type_ string `json:"type"` + Type_ SSHSpecificationType `json:"type"` + KerberosConfig *KerberosConfig + SSHConfig *SshConfig } diff --git a/harness/nextgen/model_ssh_auth.go b/harness/nextgen/model_ssh_auth.go index 81240821..1a8df615 100644 --- a/harness/nextgen/model_ssh_auth.go +++ b/harness/nextgen/model_ssh_auth.go @@ -13,5 +13,5 @@ package nextgen type SshAuth struct { Spec *BaseSshSpec `json:"spec"` // Specifies authentication scheme, SSH or Kerberos - Type_ string `json:"type"` + Type_ SSHAuthenticationType `json:"type"` } diff --git a/harness/nextgen/model_ssh_credential_spec.go b/harness/nextgen/model_ssh_credential_spec.go index b6441072..1d6aadc7 100644 --- a/harness/nextgen/model_ssh_credential_spec.go +++ b/harness/nextgen/model_ssh_credential_spec.go @@ -11,5 +11,8 @@ package nextgen // This is the SSH credential specification defined in Harness. type SshCredentialSpec struct { - CredentialType string `json:"credentialType"` + CredentialType string `json:"credentialType"` + KeyPathCredential *SshKeyPathCredential `json:"keyPathCredential"` + KeyReferenceCredential *SshKeyReferenceCredentialDto `json:"keyReferenceCredential"` + PasswordCredential *SshPasswordCredentialDto `json:"passwordCredential"` } diff --git a/harness/nextgen/model_ssh_key_spec.go b/harness/nextgen/model_ssh_key_spec.go index 4422c261..82ccca95 100644 --- a/harness/nextgen/model_ssh_key_spec.go +++ b/harness/nextgen/model_ssh_key_spec.go @@ -11,8 +11,8 @@ package nextgen // This is the SSH key authentication details defined in Harness. type SshKeySpec struct { - ErrorMessageForInvalidYaml string `json:"errorMessageForInvalidYaml,omitempty"` - Type_ string `json:"type"` + ErrorMessageForInvalidYaml string `json:"errorMessageForInvalidYaml,omitempty"` + Type_ SecretSpecType `json:"type"` // SSH port Port int32 `json:"port,omitempty"` Auth *SshAuth `json:"auth"` diff --git a/harness/nextgen/model_tgt_generation_spec_dto.go b/harness/nextgen/model_tgt_generation_spec_dto.go index ef8868ea..bbef8c10 100644 --- a/harness/nextgen/model_tgt_generation_spec_dto.go +++ b/harness/nextgen/model_tgt_generation_spec_dto.go @@ -10,5 +10,7 @@ package nextgen type TgtGenerationSpecDto struct { - TgtGenerationMethod string `json:"tgtGenerationMethod"` + TgtGenerationMethod string `json:"tgtGenerationMethod"` + KeyTabFilePathSpec *TgtKeyTabFilePathSpecDto `` + PasswordSpec *TgtPasswordSpecDto } From 4ce3c2e31009b3f2da058fd1b5a17f0562477f8c Mon Sep 17 00:00:00 2001 From: Meet Rathod Date: Thu, 30 Jun 2022 00:13:09 +0530 Subject: [PATCH 2/2] change model and enums for ssh key secret type --- harness/nextgen/enum_ssh_config_type.go | 23 +++++++ .../enum_tgt_generation_method_type.go | 20 ++++++ harness/nextgen/model_base_ssh_spec.go | 7 ++- harness/nextgen/model_kerberos_config.go | 10 ++- ...odel_kerberos_config_spec_serialization.go | 57 +++++++++++++++++ harness/nextgen/model_secret_serializer.go | 2 +- harness/nextgen/model_ssh_auth.go | 6 +- harness/nextgen/model_ssh_auth_serializer.go | 57 +++++++++++++++++ harness/nextgen/model_ssh_config.go | 11 +++- .../nextgen/model_ssh_config_serialization.go | 61 +++++++++++++++++++ .../model_ssh_specification_serializer.go | 57 +++++++++++++++++ .../nextgen/model_tgt_generation_spec_dto.go | 5 +- .../model_tgt_generation_spec_serializer.go | 57 +++++++++++++++++ 13 files changed, 362 insertions(+), 11 deletions(-) create mode 100644 harness/nextgen/enum_ssh_config_type.go create mode 100644 harness/nextgen/enum_tgt_generation_method_type.go create mode 100644 harness/nextgen/model_kerberos_config_spec_serialization.go create mode 100644 harness/nextgen/model_ssh_auth_serializer.go create mode 100644 harness/nextgen/model_ssh_config_serialization.go create mode 100644 harness/nextgen/model_ssh_specification_serializer.go create mode 100644 harness/nextgen/model_tgt_generation_spec_serializer.go diff --git a/harness/nextgen/enum_ssh_config_type.go b/harness/nextgen/enum_ssh_config_type.go new file mode 100644 index 00000000..555aac86 --- /dev/null +++ b/harness/nextgen/enum_ssh_config_type.go @@ -0,0 +1,23 @@ +package nextgen + +type SSHConfigType string + +var SSHConfigTypes = struct { + Password SSHConfigType + KeyPath SSHConfigType + KeyReference SSHConfigType +}{ + Password: "Password", + KeyPath: "KeyPath", + KeyReference: "KeyReference", +} + +var SSHConfigTypeValues = []string{ + SSHConfigTypes.Password.String(), + SSHConfigTypes.KeyPath.String(), + SSHConfigTypes.KeyReference.String(), +} + +func (e SSHConfigType) String() string { + return string(e) +} diff --git a/harness/nextgen/enum_tgt_generation_method_type.go b/harness/nextgen/enum_tgt_generation_method_type.go new file mode 100644 index 00000000..cdd1a7bc --- /dev/null +++ b/harness/nextgen/enum_tgt_generation_method_type.go @@ -0,0 +1,20 @@ +package nextgen + +type TgtGenerationMethodType string + +var TgtGenerationMethodTypes = struct { + TGTKeyTabFilePathSpecDTO TgtGenerationMethodType + TGTPasswordSpecDTO TgtGenerationMethodType +}{ + TGTKeyTabFilePathSpecDTO: "KeyTabFilePath", + TGTPasswordSpecDTO: "Password", +} + +var TgtGenerationMethodTypeValues = []string{ + TgtGenerationMethodTypes.TGTKeyTabFilePathSpecDTO.String(), + TgtGenerationMethodTypes.TGTPasswordSpecDTO.String(), +} + +func (e TgtGenerationMethodType) String() string { + return string(e) +} diff --git a/harness/nextgen/model_base_ssh_spec.go b/harness/nextgen/model_base_ssh_spec.go index 504b0951..e0a4a0db 100644 --- a/harness/nextgen/model_base_ssh_spec.go +++ b/harness/nextgen/model_base_ssh_spec.go @@ -9,9 +9,12 @@ */ package nextgen +import "encoding/json" + // This is the SSH specification details as defined in Harness. type BaseSshSpec struct { Type_ SSHSpecificationType `json:"type"` - KerberosConfig *KerberosConfig - SSHConfig *SshConfig + KerberosConfig *KerberosConfig `json:"-"` + SSHConfig *SshConfig `json:"-"` + Spec json.RawMessage `json:"spec"` } diff --git a/harness/nextgen/model_kerberos_config.go b/harness/nextgen/model_kerberos_config.go index 81982081..81575ae1 100644 --- a/harness/nextgen/model_kerberos_config.go +++ b/harness/nextgen/model_kerberos_config.go @@ -9,13 +9,17 @@ */ package nextgen +import "encoding/json" + // This is the Kerberos configuration details, defined in Harness. type KerberosConfig struct { Type_ string `json:"type"` // This is the authorization role, the user/service has in the realm. Principal string `json:"principal"` // Name of the Realm. - Realm string `json:"realm"` - TgtGenerationMethod string `json:"tgtGenerationMethod,omitempty"` - Spec *TgtGenerationSpecDto `json:"spec,omitempty"` + Realm string `json:"realm"` + TgtGenerationMethod TgtGenerationMethodType `json:"tgtGenerationMethod,omitempty"` + KeyTabFilePathSpec *TgtKeyTabFilePathSpecDto `json:"-"` + PasswordSpec *TgtPasswordSpecDto `json:"-"` + Spec json.RawMessage `json:"spec,omitempty"` } diff --git a/harness/nextgen/model_kerberos_config_spec_serialization.go b/harness/nextgen/model_kerberos_config_spec_serialization.go new file mode 100644 index 00000000..11407a7b --- /dev/null +++ b/harness/nextgen/model_kerberos_config_spec_serialization.go @@ -0,0 +1,57 @@ +package nextgen + +import ( + "encoding/json" + "fmt" +) + +func (a *KerberosConfig) UnmarshalJSON(data []byte) error { + + type Alias KerberosConfig + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + switch a.TgtGenerationMethod { + case TgtGenerationMethodTypes.TGTKeyTabFilePathSpecDTO: + err = json.Unmarshal(aux.Spec, &a.KeyTabFilePathSpec) + case TgtGenerationMethodTypes.TGTPasswordSpecDTO: + err = json.Unmarshal(aux.Spec, &a.PasswordSpec) + default: + panic(fmt.Sprintf("unknown Tgt generation method type %s", a.TgtGenerationMethod)) + } + + return err +} + +func (a *KerberosConfig) MarshalJSON() ([]byte, error) { + type Alias KerberosConfig + + var spec []byte + var err error + + switch a.TgtGenerationMethod { + case TgtGenerationMethodTypes.TGTKeyTabFilePathSpecDTO: + spec, err = json.Marshal(a.KeyTabFilePathSpec) + case TgtGenerationMethodTypes.TGTPasswordSpecDTO: + spec, err = json.Marshal(a.PasswordSpec) + default: + panic(fmt.Sprintf("unknown Tgt generation method type %s", a.TgtGenerationMethod)) + } + + if err != nil { + return nil, err + } + + a.Spec = json.RawMessage(spec) + + return json.Marshal((*Alias)(a)) +} diff --git a/harness/nextgen/model_secret_serializer.go b/harness/nextgen/model_secret_serializer.go index ffc35f5a..16bbc025 100644 --- a/harness/nextgen/model_secret_serializer.go +++ b/harness/nextgen/model_secret_serializer.go @@ -44,7 +44,7 @@ func (a *Secret) MarshalJSON() ([]byte, error) { case SecretTypes.SecretFile: spec, err = json.Marshal(a.File) case SecretTypes.SSHKey: - // spec, err = json.Marshal(a.AssumeIamRole) + spec, err = json.Marshal(a.SSHKey) // noop case SecretTypes.SecretText: spec, err = json.Marshal(a.Text) diff --git a/harness/nextgen/model_ssh_auth.go b/harness/nextgen/model_ssh_auth.go index 1a8df615..715b2e5e 100644 --- a/harness/nextgen/model_ssh_auth.go +++ b/harness/nextgen/model_ssh_auth.go @@ -9,9 +9,13 @@ */ package nextgen +import "encoding/json" + // This is the SSH Authentication specification defined in Harness. type SshAuth struct { - Spec *BaseSshSpec `json:"spec"` + KerberosConfig *KerberosConfig `json:"-"` + SSHConfig *SshConfig `json:"-"` + Spec json.RawMessage `json:"spec"` // Specifies authentication scheme, SSH or Kerberos Type_ SSHAuthenticationType `json:"type"` } diff --git a/harness/nextgen/model_ssh_auth_serializer.go b/harness/nextgen/model_ssh_auth_serializer.go new file mode 100644 index 00000000..53f60a8f --- /dev/null +++ b/harness/nextgen/model_ssh_auth_serializer.go @@ -0,0 +1,57 @@ +package nextgen + +import ( + "encoding/json" + "fmt" +) + +func (a *SshAuth) UnmarshalJSON(data []byte) error { + + type Alias SshAuth + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + switch a.Type_ { + case SSHAuthenticationTypes.Kerberos: + err = json.Unmarshal(aux.Spec, &a.KerberosConfig) + case SSHAuthenticationTypes.SSH: + err = json.Unmarshal(aux.Spec, &a.SSHConfig) + default: + panic(fmt.Sprintf("unknown SSH authentication type %s", a.Type_)) + } + + return err +} + +func (a *SshAuth) MarshalJSON() ([]byte, error) { + type Alias SshAuth + + var spec []byte + var err error + + switch a.Type_ { + case SSHAuthenticationTypes.Kerberos: + spec, err = json.Marshal(a.KerberosConfig) + case SSHAuthenticationTypes.SSH: + spec, err = json.Marshal(a.SSHConfig) + default: + panic(fmt.Sprintf("unknown SSH authentication type %s", a.Type_)) + } + + if err != nil { + return nil, err + } + + a.Spec = json.RawMessage(spec) + + return json.Marshal((*Alias)(a)) +} diff --git a/harness/nextgen/model_ssh_config.go b/harness/nextgen/model_ssh_config.go index 9eba051f..7133e85e 100644 --- a/harness/nextgen/model_ssh_config.go +++ b/harness/nextgen/model_ssh_config.go @@ -9,10 +9,15 @@ */ package nextgen +import "encoding/json" + // This is the SSH configuration details defined in Harness. type SshConfig struct { - Type_ string `json:"type"` + Type_ SSHConfigType `json:"type"` // This specifies SSH credential type as Password, KeyPath or KeyReference - CredentialType string `json:"credentialType"` - Spec *SshCredentialSpec `json:"spec"` + CredentialType SSHConfigType `json:"credentialType"` + KeyReferenceCredential *SshKeyReferenceCredentialDto `json:"-"` + KeyPathCredential *SshKeyPathCredential `json:"-"` + PasswordCredential *SshPasswordCredentialDto `json:"-"` + Spec json.RawMessage `json:"spec"` } diff --git a/harness/nextgen/model_ssh_config_serialization.go b/harness/nextgen/model_ssh_config_serialization.go new file mode 100644 index 00000000..a1b0eb0f --- /dev/null +++ b/harness/nextgen/model_ssh_config_serialization.go @@ -0,0 +1,61 @@ +package nextgen + +import ( + "encoding/json" + "fmt" +) + +func (a *SshConfig) UnmarshalJSON(data []byte) error { + + type Alias SshConfig + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + switch a.CredentialType { + case SSHConfigTypes.KeyPath: + err = json.Unmarshal(aux.Spec, &a.KeyPathCredential) + case SSHConfigTypes.KeyReference: + err = json.Unmarshal(aux.Spec, &a.KeyReferenceCredential) + case SSHConfigTypes.Password: + err = json.Unmarshal(aux.Spec, &a.PasswordCredential) + default: + panic(fmt.Sprintf("unknown SSH config type %s", a.Type_)) + } + + return err +} + +func (a *SshConfig) MarshalJSON() ([]byte, error) { + type Alias SshConfig + + var spec []byte + var err error + + switch a.CredentialType { + case SSHConfigTypes.KeyPath: + spec, err = json.Marshal(a.KeyPathCredential) + case SSHConfigTypes.KeyReference: + spec, err = json.Marshal(a.KeyReferenceCredential) + case SSHConfigTypes.Password: + spec, err = json.Marshal(a.PasswordCredential) + default: + panic(fmt.Sprintf("unknown SSH config type %s", a.Type_)) + } + + if err != nil { + return nil, err + } + + a.Spec = json.RawMessage(spec) + + return json.Marshal((*Alias)(a)) +} diff --git a/harness/nextgen/model_ssh_specification_serializer.go b/harness/nextgen/model_ssh_specification_serializer.go new file mode 100644 index 00000000..ad7f4a4a --- /dev/null +++ b/harness/nextgen/model_ssh_specification_serializer.go @@ -0,0 +1,57 @@ +package nextgen + +import ( + "encoding/json" + "fmt" +) + +func (a *BaseSshSpec) UnmarshalJSON(data []byte) error { + + type Alias BaseSshSpec + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + switch a.Type_ { + case SSHSpecificationTypes.KerberosConfigDTO: + err = json.Unmarshal(aux.Spec, &a.KerberosConfig) + case SSHSpecificationTypes.SSHConfig: + err = json.Unmarshal(aux.Spec, &a.SSHConfig) + default: + panic(fmt.Sprintf("unknown SSH specification type %s", a.Type_)) + } + + return err +} + +func (a *BaseSshSpec) MarshalJSON() ([]byte, error) { + type Alias BaseSshSpec + + var spec []byte + var err error + + switch a.Type_ { + case SSHSpecificationTypes.KerberosConfigDTO: + spec, err = json.Marshal(a.KerberosConfig) + case SSHSpecificationTypes.SSHConfig: + spec, err = json.Marshal(a.SSHConfig) + default: + panic(fmt.Sprintf("unknown SSH specification type %s", a.Type_)) + } + + if err != nil { + return nil, err + } + + a.Spec = json.RawMessage(spec) + + return json.Marshal((*Alias)(a)) +} diff --git a/harness/nextgen/model_tgt_generation_spec_dto.go b/harness/nextgen/model_tgt_generation_spec_dto.go index bbef8c10..82857a61 100644 --- a/harness/nextgen/model_tgt_generation_spec_dto.go +++ b/harness/nextgen/model_tgt_generation_spec_dto.go @@ -9,8 +9,11 @@ */ package nextgen +import "encoding/json" + type TgtGenerationSpecDto struct { - TgtGenerationMethod string `json:"tgtGenerationMethod"` + TgtGenerationMethod TgtGenerationMethodType `json:"tgtGenerationMethod"` KeyTabFilePathSpec *TgtKeyTabFilePathSpecDto `` PasswordSpec *TgtPasswordSpecDto + Spec json.RawMessage `json:"spec"` } diff --git a/harness/nextgen/model_tgt_generation_spec_serializer.go b/harness/nextgen/model_tgt_generation_spec_serializer.go new file mode 100644 index 00000000..44babf51 --- /dev/null +++ b/harness/nextgen/model_tgt_generation_spec_serializer.go @@ -0,0 +1,57 @@ +package nextgen + +import ( + "encoding/json" + "fmt" +) + +func (a *TgtGenerationSpecDto) UnmarshalJSON(data []byte) error { + + type Alias TgtGenerationSpecDto + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + switch a.TgtGenerationMethod { + case TgtGenerationMethodTypes.TGTKeyTabFilePathSpecDTO: + err = json.Unmarshal(aux.Spec, &a.KeyTabFilePathSpec) + case TgtGenerationMethodTypes.TGTPasswordSpecDTO: + err = json.Unmarshal(aux.Spec, &a.PasswordSpec) + default: + panic(fmt.Sprintf("unknown Tgt generation method type %s", a.TgtGenerationMethod)) + } + + return err +} + +func (a *TgtGenerationSpecDto) MarshalJSON() ([]byte, error) { + type Alias TgtGenerationSpecDto + + var spec []byte + var err error + + switch a.TgtGenerationMethod { + case TgtGenerationMethodTypes.TGTKeyTabFilePathSpecDTO: + spec, err = json.Marshal(a.KeyTabFilePathSpec) + case TgtGenerationMethodTypes.TGTPasswordSpecDTO: + spec, err = json.Marshal(a.PasswordSpec) + default: + panic(fmt.Sprintf("unknown Tgt generation method type %s", a.TgtGenerationMethod)) + } + + if err != nil { + return nil, err + } + + a.Spec = json.RawMessage(spec) + + return json.Marshal((*Alias)(a)) +}