// Cray cert replacement.  This also works with RTS PDUs.
//
// Algo:
//
// o Get the contents of /redfish/v1/CertificateService to verify the target
//   will take a PEM cert, and that it will do a cert replace operation.
//   Also get the "action" URI.
// o Get contents of /redfish/v1/CertificateService/CertificateLocations to
//   get the URI of the current cert.  There may be multiples -- use the
//   highest numbered one??
// o POST the new cert using that URI (e.g. /redfish/v1/Managers/BMC/NetworkProtocol/HTTPS/Certificates/1)
//   at the action URI /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
// o May need to deal with Etags too!
Cray cert replacement.  This also works with RTS PDUs.



o Get the contents of /redfish/v1/CertificateService to verify the target
will take a PEM cert, and that it will do a cert replace operation.
Also get the "action" URI.
o Get contents of /redfish/v1/CertificateService/CertificateLocations to
get the URI of the current cert.
// Cray cert replacement . This also works with RTS PDUs . // // Algo : // // o Get the contents of / redfish / v1 / CertificateService to verify the target // will take a PEM cert and that it will do a cert replace operation . // Also get the " action " URI . // o Get contents of / redfish / v1 / CertificateService / CertificateLocations to // get the URI of the current cert . There may be multiples -- use the // highest numbered one?? // o POST the new cert using that URI ( e . g . / redfish / v1 / Managers / BMC / NetworkProtocol / HTTPS / Certificates / 1 ) // at the action URI / redfish / v1 / CertificateService / Actions / CertificateService . ReplaceCertificate // o May need to deal with Etags too!
func doCrayCerts(taskList []trsapi.HttpTask, targList []string, certs []bmcCertData) error {
	var err error
	funcName := "doCrayCerts()"
	logger.Tracef("%s: Fetching CertificateService data.", funcName)
	populateTaskList(taskList, targList, CRAY_CERTSVC_API, "GET", nil)
	err = doOp(taskList)
	if err != nil {
		logger.Errorf("%s: Problem executing cert set: %v",
			funcName, err)
		return err
	}
	ignoreBadTasks(funcName, taskList)
	actionURIs := make([]string, len(taskList))
	logger.Tracef("%s: Parsing Chassis data, setting up for CertificateLocation data.",
		funcName)
	for ii := 0; ii < len(taskList); ii++ {
		if taskList[ii].Ignore {
			continue
		}
		var jdata crayCertificateService
		targ := targFromTask(&taskList[ii])
		err = grabTaskRspData(funcName, &taskList[ii], &jdata)
		if err != nil {
			logger.Errorf("%s: Problem getting response from '%s': %v",
				funcName, taskList[ii].Request.URL.Path, err)
			return err
		}
		actionURIs[ii] = jdata.Actions.ReplaceCert.Target
		url := dfltProtocol + ":" + targ + jdata.CertificateLocations.ID
		taskList[ii].Request, _ = http.NewRequest("GET", url, nil)
	}
	err = doOp(taskList)
	if err != nil {
		logger.Errorf("%s: Problem executing cert service data: %v",
			funcName, err)
		return err
	}
	ignoreBadTasks(funcName, taskList)
	logger.Tracef("%s: Parsing cert location data, set up for cert replace.",
		funcName)
	for ii := 0; ii < len(taskList); ii++ {
		if taskList[ii].Ignore {
			continue
		}
		var jdata crayCertificateLocations
		targ := targFromTask(&taskList[ii])
		err = grabTaskRspData(funcName, &taskList[ii], &jdata)
		if err != nil {
			logger.Errorf("%s: Problem getting response from '%s': %v",
				funcName, taskList[ii].Request.URL.Path, err)
			return err
		}
		certURI := jdata.Links.Certificates[0].ID
		url := dfltProtocol + ":" + targ + actionURIs[ii]
		pld := makeRFCertPayload(VendorCray, certs[ii], certURI, "PEM")
		aaa := strings.Replace(certURI,"/Certificates","",1)
		logger.Tracef("Cray Cert URI: '%s'",aaa)
		pld := makeRFCertPayload(VendorCray,certs[ii],aaa,"PEM")
		taskList[ii].Request, _ = http.NewRequest("POST", url, bytes.NewBuffer(pld))
		taskList[ii].Request.Header.Set(CT_TYPE, CT_APPJSON)
		/*etag := jdata.Etag	might be empty, that's OK
		if (etag != nil) {
			taskList[ii].Request.Header.Add(ET_IFNONE,fixEtag(etag))
		}*/
		logger.Tracef("%s: url: '%s', '%s'",
			funcName, url, taskList[ii].Request.URL.Path)
	}
	Do the POST to write the new certs
	err = doOp(taskList)
	if err != nil {
		logger.Errorf("%s: Problem setting certificate: %v", funcName, err)
		return err
	}
	ignoreBadTasks(funcName, taskList)
	logger.Tracef("%s: Finished cert replacement.", funcName)
	return nil
}
func doCrayCerts ( taskList [ ] trsapi . HttpTask , targList [ ] string , certs [ ] bmcCertData ) error { var err error funcName := " " logger . Tracef ( " " , funcName ) populateTaskList ( taskList , targList , CRAY_CERTSVC_API , " " , nil ) err = doOp ( taskList ) if err != nil { logger . Errorf ( " " , funcName , err ) return err } ignoreBadTasks ( funcName , taskList ) actionURIs := make ( [ ] string , len ( taskList ) ) logger . Tracef ( " " , funcName ) for ii := 0 ; ii < len ( taskList ) ; ii ++ { if taskList [ ii ] . Ignore { continue } var jdata crayCertificateService targ := targFromTask ( & taskList [ ii ] ) err = grabTaskRspData ( funcName , & taskList [ ii ] , & jdata ) if err != nil { logger . Errorf ( " " , funcName , taskList [ ii ] . Request . URL . Path , err ) return err } actionURIs [ ii ] = jdata . Actions . ReplaceCert . Target url := dfltProtocol + " " + targ + jdata . CertificateLocations . ID taskList [ ii ] . Request , _ = http . NewRequest ( " " , url , nil ) } err = doOp ( taskList ) if err != nil { logger . Errorf ( " " , funcName , err ) return err } ignoreBadTasks ( funcName , taskList ) logger . Tracef ( " " , funcName ) for ii := 0 ; ii < len ( taskList ) ; ii ++ { if taskList [ ii ] . Ignore { continue } var jdata crayCertificateLocations targ := targFromTask ( & taskList [ ii ] ) err = grabTaskRspData ( funcName , & taskList [ ii ] , & jdata ) if err != nil { logger . Errorf ( " " , funcName , taskList [ ii ] . Request . URL . Path , err ) return err } certURI := jdata . Links . Certificates [ 0 ] . ID url := dfltProtocol + " " + targ + actionURIs [ ii ] pld := makeRFCertPayload ( VendorCray , certs [ ii ] , certURI , " " ) taskList [ ii ] . Request , _ = http . NewRequest ( " " , url , bytes . NewBuffer ( pld ) ) taskList [ ii ] . Request . Header . Set ( CT_TYPE , CT_APPJSON ) logger . Tracef ( " " , funcName , url , taskList [ ii ] . Request . URL . Path ) } err = doOp ( taskList ) if err != nil { logger . Errorf ( " " , funcName , err ) return err } ignoreBadTasks ( funcName , taskList ) logger . Tracef ( " " , funcName ) return nil }
Cray cert replacement.
Cray cert replacement .
//First check /redfish/v1/CertificateService to verify we're using PEM //certs and that we have the cert replacement URI //Parse the results, set up the next stage. //etags := make([]string,len(taskList)) //Get the action URI and the CertificateLocations URI //Parse the cert location URI, set stage for the cert write. //Create URL and payload for writing cert.  Note that Cray only //supports 1 cert URI even though it's represented as an array. //TODO: this may change eventually. //This is needed for testing with older mountain BMC FW which //uses a different URL than the CertificateLocations says. // //aaa := strings.Replace(certURI,"/Certificates","",1) //logger.Tracef("Cray Cert URI: '%s'",aaa) //pld := makeRFCertPayload(VendorCray,certs[ii],aaa,"PEM") /*etag := jdata.Etag	//might be empty, that's OK
		if (etag != nil) {
			taskList[ii].Request.Header.Add(ET_IFNONE,fixEtag(etag))
		}*/ //Do the POST to write the new certs