// Fetch leaf cert from Vault and apply it to a single RF target.
Fetch leaf cert from Vault and apply it to a single RF target.
// Fetch leaf cert from Vault and apply it to a single RF target .
func doBMCSetCertsPostSingle(w http.ResponseWriter, r *http.Request) {
	var certDomain string
	var retData rfCertPostRsp
	funcName := "doBMCSetCertsPostSingle"
	vars := mux.Vars(r)
	targ := base.NormalizeHMSCompID(vars["xname"])
	force, cdom := getRFPostParams(r)
	td := makeTargData([]string{targ})
	_, tderr := hsmVerify(td, force, false)
	if tderr != nil {
		emsg := fmt.Sprintf("ERROR: Invalid xname: '%s'.",
			vars["xname"])
		sendErrorRsp(w, "Bad target name", emsg, r.URL.Path,
			http.StatusBadRequest)
		return
	}
	switch strings.ToLower(cdom) {
	case "cabinet":
		certDomain = hms_certs.CertDomainCabinet
	case "chassis":
		certDomain = hms_certs.CertDomainChassis
	case "blade":
		certDomain = hms_certs.CertDomainBlade
	case "bmc":
		certDomain = hms_certs.CertDomainBMC
	default:
		emsg := fmt.Sprintf("ERROR: Invalid cert domain: '%s'.",
			cdom)
		sendErrorRsp(w, "Bad cert domain", emsg, r.URL.Path,
			http.StatusBadRequest)
		return
	}
	domID, err := hms_certs.CheckDomain([]string{targ}, certDomain)
	if err != nil {
		emsg := fmt.Sprintf("Cert target %s not found in domain %s",
			targ, cdom)
		sendErrorRsp(w, "Bad cert domain", emsg, r.URL.Path,
			http.StatusBadRequest)
		logger.Errorf("%s: %s", funcName, emsg)
		return
	}
	vcert, vcerr := hms_certs.FetchCertData(domID, certDomain)
	if vcerr != nil {
		emsg := fmt.Sprintf("ERROR fetching cert for '%s', domain '%s': %v",
			targ, cdom, vcerr)
		sendErrorRsp(w, "Bad cert domain", emsg, r.URL.Path,
			http.StatusInternalServerError)
		logger.Errorf("%s: %s", funcName, emsg)
		return
	}
	var tlist = []string{targ}
	var sourceTL trsapi.HttpTask
	sourceTL.Timeout = time.Duration(appParams.HTTPTimeout) * time.Second
	sourceTL.Request, _ = http.NewRequest(http.MethodGet, "", nil)
	taskList := tloc.CreateTaskList(&sourceTL, len(tlist))
	populateTaskList(taskList, tlist, RFROOT_API, http.MethodGet, nil)
	certs := make([]bmcCertData, 1)
	certs[0].Cert = vcert.Data.Certificate
	certs[0].Key = vcert.Data.PrivateKey
	certErr := setCerts(taskList, certs, &retData)
	if certErr != nil {
		emsg := fmt.Sprintf("ERROR: Certificate set operation failed: %v",
			certErr)
		sendErrorRsp(w, "Certificate set operation error", emsg, r.URL.Path,
			http.StatusBadRequest)
		logger.Errorf("%s: %s", funcName, emsg)
		return
	}
	ba, berr := json.Marshal(&retData.Targets[0])
	if berr != nil {
		emsg := fmt.Sprintf("ERROR: Problem marshaling return data.")
		sendErrorRsp(w, "JSON data marshal error", emsg, r.URL.Path,
			http.StatusInternalServerError)
		logger.Errorf("%s: %s", funcName, emsg)
		return
	}
	w.Header().Set(CT_TYPE, CT_APPJSON)
	w.WriteHeader(retData.Targets[0].StatusCode)
	w.Write(ba)
}
func doBMCSetCertsPostSingle ( w http . ResponseWriter , r * http . Request ) { var certDomain string var retData rfCertPostRsp funcName := " " vars := mux . Vars ( r ) targ := base . NormalizeHMSCompID ( vars [ " " ] ) force , cdom := getRFPostParams ( r ) td := makeTargData ( [ ] string { targ } ) _ , tderr := hsmVerify ( td , force , false ) if tderr != nil { emsg := fmt . Sprintf ( " " , vars [ " " ] ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusBadRequest ) return } switch strings . ToLower ( cdom ) { case " " : certDomain = hms_certs . CertDomainCabinet case " " : certDomain = hms_certs . CertDomainChassis case " " : certDomain = hms_certs . CertDomainBlade case " " : certDomain = hms_certs . CertDomainBMC default : emsg := fmt . Sprintf ( " " , cdom ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusBadRequest ) return } domID , err := hms_certs . CheckDomain ( [ ] string { targ } , certDomain ) if err != nil { emsg := fmt . Sprintf ( " " , targ , cdom ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusBadRequest ) logger . Errorf ( " " , funcName , emsg ) return } vcert , vcerr := hms_certs . FetchCertData ( domID , certDomain ) if vcerr != nil { emsg := fmt . Sprintf ( " " , targ , cdom , vcerr ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusInternalServerError ) logger . Errorf ( " " , funcName , emsg ) return } var tlist = [ ] string { targ } var sourceTL trsapi . HttpTask sourceTL . Timeout = time . Duration ( appParams . HTTPTimeout ) * time . Second sourceTL . Request , _ = http . NewRequest ( http . MethodGet , " " , nil ) taskList := tloc . CreateTaskList ( & sourceTL , len ( tlist ) ) populateTaskList ( taskList , tlist , RFROOT_API , http . MethodGet , nil ) certs := make ( [ ] bmcCertData , 1 ) certs [ 0 ] . Cert = vcert . Data . Certificate certs [ 0 ] . Key = vcert . Data . PrivateKey certErr := setCerts ( taskList , certs , & retData ) if certErr != nil { emsg := fmt . Sprintf ( " " , certErr ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusBadRequest ) logger . Errorf ( " " , funcName , emsg ) return } ba , berr := json . Marshal ( & retData . Targets [ 0 ] ) if berr != nil { emsg := fmt . Sprintf ( " " ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusInternalServerError ) logger . Errorf ( " " , funcName , emsg ) return } w . Header ( ) . Set ( CT_TYPE , CT_APPJSON ) w . WriteHeader ( retData . Targets [ 0 ] . StatusCode ) w . Write ( ba ) }
Fetch leaf cert from Vault and apply it to a single RF target.
Fetch leaf cert from Vault and apply it to a single RF target .
//Verify the target //Verify the cert domain //Resolve target to domainID. //Grab the cert from Vault //Call setCerts() to do the dirty work using the map to get the right cert //data. //Make cert/key list //Since we only return data for a single target, if it failed we will //return an error status code.