You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2021/05/07 16:33:26 UTC

[GitHub] [trafficcontrol] srijeet0406 commented on a change in pull request #5812: Implement SSLKeys methods for PostgreSQL Traffic Vault backend

srijeet0406 commented on a change in pull request #5812:
URL: https://github.com/apache/trafficcontrol/pull/5812#discussion_r628357412



##########
File path: traffic_ops/traffic_ops_golang/trafficvault/backends/postgres/postgres.go
##########
@@ -100,24 +101,149 @@ func (p *Postgres) commitTransaction(tx *sqlx.Tx, ctx context.Context, cancelFun
 	cancelFunc()
 }
 
+// GetDeliveryServiceSSLKeys retrieves the SSL keys of the given version for
+// the delivery service identified by the given xmlID. If version is empty,
+// the implementation should return the latest version.
 func (p *Postgres) GetDeliveryServiceSSLKeys(xmlID string, version string, tx *sql.Tx, ctx context.Context) (tc.DeliveryServiceSSLKeysV15, bool, error) {
-	return tc.DeliveryServiceSSLKeysV15{}, false, notImplementedErr
+	tvTx, dbCtx, cancelFunc, err := p.beginTransaction(ctx)
+	if err != nil {
+		return tc.DeliveryServiceSSLKeysV15{}, false, err
+	}
+	defer p.commitTransaction(tvTx, dbCtx, cancelFunc)
+	var jsonKeys string
+	query := "SELECT data FROM sslkey WHERE deliveryservice=$1"
+	if version != "" {
+		query += " AND version=$2"
+		err = tvTx.QueryRow(query, xmlID, version).Scan(&jsonKeys)
+	} else {
+		err = tvTx.QueryRow(query, xmlID).Scan(&jsonKeys)
+	}
+	if err != nil {
+		if err == sql.ErrNoRows {
+			return tc.DeliveryServiceSSLKeysV15{}, false, nil
+		}
+		e := checkErrWithContext("Traffic Vault PostgreSQL: executing SELECT SSL Keys query", err, ctx.Err())
+		return tc.DeliveryServiceSSLKeysV15{}, false, e
+	}
+	sslKey := tc.DeliveryServiceSSLKeysV15{}
+	err = json.Unmarshal([]byte(jsonKeys), &sslKey)
+	if err != nil {
+		return tc.DeliveryServiceSSLKeysV15{}, false, errors.New("unmarshalling ssl keys: " + err.Error())
+	}
+	return sslKey, true, nil
 }
 
+// PutDeliveryServiceSSLKeys stores the given SSL keys for a delivery service.
 func (p *Postgres) PutDeliveryServiceSSLKeys(key tc.DeliveryServiceSSLKeys, tx *sql.Tx, ctx context.Context) error {
-	return notImplementedErr
+	tvTx, dbCtx, cancelFunc, err := p.beginTransaction(ctx)
+	if err != nil {
+		return err
+	}
+	defer p.commitTransaction(tvTx, dbCtx, cancelFunc)
+
+	keyJSON, err := json.Marshal(&key)

Review comment:
       So the `version` cannot be empty in this request, the validators make sure of that before passing the request body into this method. But I've made the change so that the methos actually insert two rows, one with the specified version, and one with `latest`, after deleting the `latest` and the key with the given version (if provided)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org