Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions traffic_ops/traffic_ops_golang/cdn/dnsseckeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cdn

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import (
"database/sql"
"errors"
"net/http"

"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
)

func GetDNSSECKeys(w http.ResponseWriter, r *http.Request) {
inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"name"}, nil)
if userErr != nil || sysErr != nil {
api.HandleErr(w, r, errCode, userErr, sysErr)
return
}
defer inf.Close()

api.RespWriter(w, r)(getCDNDNSSECKeys(inf.Params["name"], inf.Tx.Tx, inf.Config))
}

func getCDNDNSSECKeys(cdnName string, tx *sql.Tx, cfg *config.Config) (tc.DNSSECKeys, error) {
keys, ok, err := riaksvc.GetDNSSECKeys(cdnName, tx, cfg.RiakAuthOptions)
if err != nil {
return tc.DNSSECKeys{}, errors.New("getting DNSSec keys from Riak: " + err.Error())
}
if !ok {
return tc.DNSSECKeys{}, nil
}
return keys, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tenant"

"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -503,7 +504,7 @@ func Delete(dbx *sqlx.DB) http.HandlerFunc {
return
}
commitTx := false
defer FinishTx(tx, &commitTx)
defer dbhelpers.FinishTx(tx, &commitTx)

count := 0
if err := db.QueryRow(`SELECT count(*) from deliveryservice_regex where deliveryservice = $1`, dsID).Scan(&count); err != nil {
Expand Down Expand Up @@ -575,15 +576,3 @@ func Delete(dbx *sqlx.DB) http.HandlerFunc {
commitTx = true
}
}

// FinishTx commits the transaction if commit is true when it's called, otherwise it rolls back the transaction. This is designed to be called in a defer.
func FinishTx(tx *sql.Tx, commit *bool) {
if tx == nil {
return
}
if !*commit {
tx.Rollback()
return
}
tx.Commit()
}
60 changes: 23 additions & 37 deletions traffic_ops/traffic_ops_golang/riaksvc/riak_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ import (
"time"

"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-tc"

"github.com/basho/riak-go-client"
"github.com/jmoiron/sqlx"
)

// RiakPort is the port RIAK is listening on.
// RiakPort is the default port RIAK is listening on, if the database tcp_port is null
const RiakPort = 8087

// 5 second timeout
Expand Down Expand Up @@ -103,13 +101,13 @@ func DeleteObject(key string, bucket string, cluster StorageCluster) error {
WithTimeout(timeOut).
Build()
if err != nil {
return err
return errors.New("riak delete: building command: " + err.Error())
}

err = cluster.Execute(cmd)

if err != nil {
return err
return errors.New("riak delete: executing command: " + err.Error())
}

return nil
Expand Down Expand Up @@ -153,11 +151,11 @@ func FetchObjectValues(key string, bucket string, cluster StorageCluster) ([]*ri
WithTimeout(timeOut).
Build()
if err != nil {
return nil, err
return nil, errors.New("riak fetch: building command: " + err.Error())
}

if err = cluster.Execute(cmd); err != nil {
return nil, err
return nil, errors.New("riak fetch: executing command: " + err.Error())
}
fvc := cmd.(*riak.FetchValueCommand)

Expand All @@ -183,11 +181,11 @@ func SaveObject(obj *riak.Object, bucket string, cluster StorageCluster) error {
WithTimeout(timeOut).
Build()
if err != nil {
return err
return errors.New("riak save: building command: " + err.Error())
}
err = cluster.Execute(cmd)
if err != nil {
return err
return errors.New("riak save: executing command: " + err.Error())
}

return nil
Expand Down Expand Up @@ -255,11 +253,12 @@ func RiakServersToCluster(servers []ServerAddr, authOptions *riak.AuthOptions) (
// returns a riak cluster of online riak nodes.
func GetRiakCluster(db *sql.DB, authOptions *riak.AuthOptions) (StorageCluster, error) {
riakServerQuery := `
SELECT s.host_name, s.domain_name FROM server s
INNER JOIN type t on s.type = t.id
INNER JOIN status st on s.status = st.id
WHERE t.name = 'RIAK' AND st.name = 'ONLINE'
`
SELECT s.host_name, s.domain_name
FROM server s
JOIN type t ON s.type = t.id
JOIN status st ON s.status = st.id
WHERE t.name = 'RIAK' AND st.name = 'ONLINE'
`

if authOptions == nil {
return nil, errors.New("ERROR: no riak auth information from riak.conf, cannot authenticate to any riak servers")
Expand All @@ -268,24 +267,27 @@ func GetRiakCluster(db *sql.DB, authOptions *riak.AuthOptions) (StorageCluster,
var nodes []*riak.Node
rows, err := db.Query(riakServerQuery)
if err != nil {
return nil, err
return nil, errors.New("querying riak servers: " + err.Error())
}
defer rows.Close()

for rows.Next() {
var s tc.Server
if err := rows.Scan(&s.HostName, &s.DomainName); err != nil {
return nil, err
host := ""
domain := ""
var n *riak.Node
if err := rows.Scan(&host, &domain); err != nil {
return nil, errors.New("scanning riak servers: " + err.Error())
}
addr := fmt.Sprintf("%s.%s:%d", s.HostName, s.DomainName, RiakPort)
port := RiakPort
addr := fmt.Sprintf("%s.%s:%d", host, domain, port)
nodeOpts := &riak.NodeOptions{
RemoteAddress: addr,
AuthOptions: authOptions,
}
nodeOpts.AuthOptions.TlsConfig.ServerName = fmt.Sprintf("%s.%s", s.HostName, s.DomainName)
nodeOpts.AuthOptions.TlsConfig.ServerName = host + "." + domain
n, err := riak.NewNode(nodeOpts)
if err != nil {
return nil, err
return nil, errors.New("creating riak node: '" + addr + "'" + err.Error())
}
nodes = append(nodes, n)
}
Expand Down Expand Up @@ -320,22 +322,6 @@ func WithCluster(db *sql.DB, authOpts *riak.AuthOptions, f func(StorageCluster)
return f(cluster)
}

func WithClusterX(db *sqlx.DB, authOpts *riak.AuthOptions, f func(StorageCluster) error) error {
cluster, err := GetRiakCluster(db.DB, authOpts)
if err != nil {
return errors.New("getting riak cluster: " + err.Error())
}
if err = cluster.Start(); err != nil {
return errors.New("starting riak cluster: " + err.Error())
}
defer func() {
if err := cluster.Stop(); err != nil {
log.Errorln("error stopping Riak cluster: " + err.Error())
}
}()
return f(cluster)
}

func GetRiakClusterTx(tx *sql.Tx, authOptions *riak.AuthOptions) (StorageCluster, error) {
servers, err := GetRiakServers(tx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion traffic_ops/traffic_ops_golang/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
//CDN: Monitoring: Traffic Monitor
{1.1, http.MethodGet, `cdns/{name}/configs/monitoring(\.json)?$`, monitoringHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},

{1.1, http.MethodGet, `cdns/name/{name}/dnsseckeys$`, cdn.GetDNSSECKeys, auth.PrivLevelOperations, Authenticated, nil},

//Division: CRUD
{1.1, http.MethodGet, `divisions/?(\.json)?$`, api.ReadHandler(division.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
{1.1, http.MethodGet, `divisions/{id}$`, api.ReadHandler(division.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
Expand Down Expand Up @@ -192,7 +194,6 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
{1.1, http.MethodDelete, `deliveryservice_server/{dsid}/{serverid}`, dsserver.Delete, auth.PrivLevelReadOnly, Authenticated, nil},

// get all edge servers associated with a delivery service (from deliveryservice_server table)

{1.1, http.MethodGet, `deliveryserviceserver$`, dsserver.ReadDSSHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
{1.1, http.MethodPost, `deliveryserviceserver$`, dsserver.GetReplaceHandler(d.DB), auth.PrivLevelOperations, Authenticated, nil},
{1.1, http.MethodPost, `deliveryservices/{xml_id}/servers$`, dsserver.GetCreateHandler(d.DB), auth.PrivLevelOperations, Authenticated, nil},
Expand Down