Skip to content

Commit

Permalink
Reduce code duplication in service configs (#2351)
Browse files Browse the repository at this point in the history
Fixes #2311. This reduces some type duplication in config tests, and makes configs more self-contained.
  • Loading branch information
scrye authored Jan 16, 2019
1 parent c36d4a5 commit 8ea849d
Show file tree
Hide file tree
Showing 29 changed files with 196 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package csconfig
package config

import (
"path/filepath"
"time"

"github.com/scionproto/scion/go/lib/as_conf"
"github.com/scionproto/scion/go/lib/env"
"github.com/scionproto/scion/go/lib/scrypto/cert"
"github.com/scionproto/scion/go/lib/truststorage"
"github.com/scionproto/scion/go/lib/util"
)

Expand All @@ -36,7 +38,21 @@ const (
ErrorCustomers = "Unable to load Customers"
)

type Conf struct {
type Config struct {
General env.General
Sciond env.SciondClient `toml:"sd_client"`
Logging env.Logging
Metrics env.Metrics
TrustDB truststorage.TrustDBConf
Infra env.Infra
CS CSConfig
}

func (c *Config) Init(confDir string) error {
return c.CS.Init(confDir)
}

type CSConfig struct {
// LeafReissueLeadTime indicates how long in advance of leaf cert expiration
// the reissuance process starts.
LeafReissueLeadTime util.DurWrap
Expand All @@ -52,7 +68,7 @@ type Conf struct {
}

// Init sets the uninitialized fields.
func (c *Conf) Init(confDir string) error {
func (c *CSConfig) Init(confDir string) error {
c.initDefaults()
if c.LeafReissueLeadTime.Duration == 0 {
if err := c.loadLeafReissTime(confDir); err != nil {
Expand All @@ -62,7 +78,7 @@ func (c *Conf) Init(confDir string) error {
return nil
}

func (c *Conf) initDefaults() {
func (c *CSConfig) initDefaults() {
if c.IssuerReissueLeadTime.Duration == 0 {
c.IssuerReissueLeadTime.Duration = IssuerReissTime
}
Expand All @@ -76,7 +92,7 @@ func (c *Conf) initDefaults() {

// loadLeafReissTime loads the as conf and sets the LeafReissTime to the PathSegmentTTL
// to provide optimal coverage.
func (c *Conf) loadLeafReissTime(confDir string) error {
func (c *CSConfig) loadLeafReissTime(confDir string) error {
if err := as_conf.Load(filepath.Join(confDir, as_conf.CfgName)); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package csconfig
package config

import (
"strings"
Expand All @@ -21,23 +21,11 @@ import (

"github.com/BurntSushi/toml"
. "github.com/smartystreets/goconvey/convey"

"github.com/scionproto/scion/go/lib/env"
"github.com/scionproto/scion/go/lib/truststorage"
)

type TestConfig struct {
General env.General
Logging env.Logging
Metrics env.Metrics
Infra env.Infra
TrustDB truststorage.TrustDBConf
CS Conf
}

func TestSampleCorrect(t *testing.T) {
Convey("Load", t, func() {
var cfg TestConfig
var cfg Config
// Make sure AutomaticRenweal is set during decoding.
cfg.CS.AutomaticRenewal = true
_, err := toml.Decode(Sample, &cfg)
Expand Down Expand Up @@ -68,7 +56,7 @@ func TestSampleCorrect(t *testing.T) {

func TestLoadConf(t *testing.T) {
Convey("Load Conf", t, func() {
var cfg TestConfig
var cfg Config
_, err := toml.DecodeFile("testdata/csconfig.toml", &cfg)
SoMsg("err", err, ShouldBeNil)
SoMsg("leafTime", cfg.CS.LeafReissueLeadTime.Duration, ShouldEqual, 7*time.Hour)
Expand All @@ -79,7 +67,7 @@ func TestLoadConf(t *testing.T) {
})

Convey("Load Default", t, func() {
var cfg TestConfig
var cfg Config
_, err := toml.DecodeReader(strings.NewReader("[cs]"), &cfg)
SoMsg("err", err, ShouldBeNil)
SoMsg("leafTime", cfg.CS.LeafReissueLeadTime.Duration, ShouldBeZeroValue)
Expand All @@ -92,12 +80,12 @@ func TestLoadConf(t *testing.T) {

func TestConfig_Init(t *testing.T) {
Convey("Load Conf", t, func() {
var cfg TestConfig
var cfg Config
_, err := toml.DecodeFile("testdata/csconfig.toml", &cfg)
SoMsg("err", err, ShouldBeNil)

Convey("Init does not override values", func() {
err := cfg.CS.Init("testdata")
err := cfg.Init("testdata")
SoMsg("err", err, ShouldBeNil)
SoMsg("leafTime", cfg.CS.LeafReissueLeadTime.Duration, ShouldEqual, 7*time.Hour)
SoMsg("issuerTime", cfg.CS.IssuerReissueLeadTime.Duration, ShouldEqual, 48*time.Hour)
Expand All @@ -108,12 +96,12 @@ func TestConfig_Init(t *testing.T) {
})

Convey("Load Default", t, func() {
var cfg TestConfig
var cfg Config
_, err := toml.DecodeReader(strings.NewReader("[cs]"), &cfg)
SoMsg("err", err, ShouldBeNil)

Convey("Init loads default values", func() {
err := cfg.CS.Init("testdata")
err := cfg.Init("testdata")
SoMsg("err", err, ShouldBeNil)
SoMsg("leafTime", cfg.CS.LeafReissueLeadTime.Duration, ShouldEqual, 6*time.Hour)
SoMsg("issuerTime", cfg.CS.IssuerReissueLeadTime.Duration, ShouldEqual, IssuerReissTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package csconfig
package config

const Sample = `[general]
# The ID of the service. This is used to choose the relevant portion of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package csconfig
package config

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package csconfig
package config

import (
"testing"
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions go/cert_srv/internal/reiss/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"net"
"time"

"github.com/scionproto/scion/go/cert_srv/internal/csconfig"
"github.com/scionproto/scion/go/cert_srv/internal/config"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl"
Expand All @@ -47,7 +47,7 @@ const (
// key in the customer mapping. Certificate chains are issued automatically by
// the issuer ASes.
type Handler struct {
State *csconfig.State
State *config.State
IA addr.IA
}

Expand Down
4 changes: 2 additions & 2 deletions go/cert_srv/internal/reiss/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"golang.org/x/crypto/ed25519"

"github.com/scionproto/scion/go/cert_srv/internal/csconfig"
"github.com/scionproto/scion/go/cert_srv/internal/config"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl"
Expand All @@ -43,7 +43,7 @@ var _ periodic.Task = (*Requester)(nil)
// expiration of the currently active certificate chain.
type Requester struct {
Msgr *messenger.Messenger
State *csconfig.State
State *config.State
IA addr.IA
LeafTime time.Duration
}
Expand Down
4 changes: 2 additions & 2 deletions go/cert_srv/internal/reiss/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"time"

"github.com/scionproto/scion/go/cert_srv/internal/csconfig"
"github.com/scionproto/scion/go/cert_srv/internal/config"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl"
Expand All @@ -40,7 +40,7 @@ var _ periodic.Task = (*Self)(nil)
type Self struct {
// Msgr is used to propagate key updates to the messenger, and not for network traffic
Msgr *messenger.Messenger
State *csconfig.State
State *config.State
IA addr.IA
IssTime time.Duration
LeafTime time.Duration
Expand Down
59 changes: 24 additions & 35 deletions go/cert_srv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/BurntSushi/toml"

"github.com/scionproto/scion/go/cert_srv/internal/csconfig"
"github.com/scionproto/scion/go/cert_srv/internal/config"
"github.com/scionproto/scion/go/cert_srv/internal/reiss"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/env"
Expand All @@ -33,22 +33,11 @@ import (
"github.com/scionproto/scion/go/lib/infra/modules/itopo"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/periodic"
"github.com/scionproto/scion/go/lib/truststorage"
)

type Config struct {
General env.General
Sciond env.SciondClient `toml:"sd_client"`
Logging env.Logging
Metrics env.Metrics
TrustDB truststorage.TrustDBConf
Infra env.Infra
CS csconfig.Conf
state *csconfig.State
}

var (
config Config
cfg config.Config
state *config.State
environment *env.Env
reissRunner *periodic.Runner
msgr *messenger.Messenger
Expand All @@ -67,15 +56,15 @@ func realMain() int {
fatal.Init()
env.AddFlags()
flag.Parse()
if v, ok := env.CheckFlags(csconfig.Sample); !ok {
if v, ok := env.CheckFlags(config.Sample); !ok {
return v
}
if err := setupBasic(); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer log.Flush()
defer env.LogAppStopped(common.CS, config.General.ID)
defer env.LogAppStopped(common.CS, cfg.General.ID)
defer log.LogPanicAndExit()
// Setup the state and the messenger
if err := setup(); err != nil {
Expand All @@ -90,14 +79,14 @@ func realMain() int {
msgr.ListenAndServe()
}()
// Set environment to listen for signals.
environment = infraenv.InitInfraEnvironmentFunc(config.General.TopologyPath, func() {
environment = infraenv.InitInfraEnvironmentFunc(cfg.General.TopologyPath, func() {
if err := reload(); err != nil {
log.Error("Unable to reload", "err", err)
}
})
// Cleanup when the CS exits.
defer stop()
config.Metrics.StartPrometheus()
cfg.Metrics.StartPrometheus()
select {
case <-environment.AppShutdownSignal:
// Whenever we receive a SIGINT or SIGTERM we exit without an error.
Expand All @@ -111,16 +100,16 @@ func realMain() int {
func reload() error {
// FIXME(roosd): KeyConf reloading is not yet supported.
// https://github.com/scionproto/scion/issues/2077
config.General.Topology = itopo.GetCurrentTopology()
var newConf Config
cfg.General.Topology = itopo.GetCurrentTopology()
var newConf config.Config
// Load new config to get the CS parameters.
if _, err := toml.DecodeFile(env.ConfigFile(), &newConf); err != nil {
return err
}
if err := newConf.CS.Init(config.General.ConfigDir); err != nil {
if err := newConf.CS.Init(cfg.General.ConfigDir); err != nil {
return common.NewBasicError("Unable to initialize CS config", err)
}
config.CS = newConf.CS
cfg.CS = newConf.CS
// Restart the periodic reissue task to respect the fresh parameters.
stopReissRunner()
startReissRunner()
Expand All @@ -130,35 +119,35 @@ func reload() error {
// startReissRunner starts a periodic reissuance task. Core starts self-issuer.
// Non-core starts a requester.
func startReissRunner() {
if !config.CS.AutomaticRenewal {
if !cfg.CS.AutomaticRenewal {
log.Info("Reissue disabled, not starting reiss task.")
return
}
if config.General.Topology.Core {
if cfg.General.Topology.Core {
log.Info("Starting periodic reiss.Self task")
reissRunner = periodic.StartPeriodicTask(
&reiss.Self{
Msgr: msgr,
State: config.state,
IA: config.General.Topology.ISD_AS,
IssTime: config.CS.IssuerReissueLeadTime.Duration,
LeafTime: config.CS.LeafReissueLeadTime.Duration,
State: state,
IA: cfg.General.Topology.ISD_AS,
IssTime: cfg.CS.IssuerReissueLeadTime.Duration,
LeafTime: cfg.CS.LeafReissueLeadTime.Duration,
},
periodic.NewTicker(config.CS.ReissueRate.Duration),
config.CS.ReissueTimeout.Duration,
periodic.NewTicker(cfg.CS.ReissueRate.Duration),
cfg.CS.ReissueTimeout.Duration,
)
return
}
log.Info("Starting periodic reiss.Requester task")
reissRunner = periodic.StartPeriodicTask(
&reiss.Requester{
Msgr: msgr,
State: config.state,
IA: config.General.Topology.ISD_AS,
LeafTime: config.CS.LeafReissueLeadTime.Duration,
State: state,
IA: cfg.General.Topology.ISD_AS,
LeafTime: cfg.CS.LeafReissueLeadTime.Duration,
},
periodic.NewTicker(config.CS.ReissueRate.Duration),
config.CS.ReissueTimeout.Duration,
periodic.NewTicker(cfg.CS.ReissueRate.Duration),
cfg.CS.ReissueTimeout.Duration,
)
}

Expand Down
Loading

0 comments on commit 8ea849d

Please sign in to comment.