func setDefaultConfigValuesWithViper(v *viper.Viper, b *BgpConfigSet) error { if v == nil { v = viper.New() } if err := SetDefaultGlobalConfigValues(&b.Global); err != nil { return err } for idx, server := range b.BmpServers { if server.Config.Port == 0 { server.Config.Port = bmp.BMP_DEFAULT_PORT } b.BmpServers[idx] = server } if b.Zebra.Config.Url == "" { b.Zebra.Config.Url = "unix:/var/run/quagga/zserv.api" } list, err := extractArray(v.Get("neighbors")) if err != nil { return err } for idx, n := range b.Neighbors { vv := viper.New() if len(list) > idx { vv.Set("neighbor", list[idx]) } if err := setDefaultNeighborConfigValuesWithViper(vv, &n, b.Global.Config.As); err != nil { return err } b.Neighbors[idx] = n } for idx, r := range b.RpkiServers { if r.Config.Port == 0 { b.RpkiServers[idx].Config.Port = rtr.RPKI_DEFAULT_PORT } } list, err = extractArray(v.Get("policy-definitions")) if err != nil { return err } for idx, p := range b.PolicyDefinitions { vv := viper.New() if len(list) > idx { vv.Set("policy", list[idx]) } if err := setDefaultPolicyConfigValuesWithViper(vv, &p); err != nil { return err } b.PolicyDefinitions[idx] = p } return nil }
func getKeysRecursively(base string, v *viper.Viper, nodeKeys map[string]interface{}) map[string]interface{} { result := make(map[string]interface{}) for key := range nodeKeys { fqKey := base + key val := v.Get(fqKey) if m, ok := val.(map[interface{}]interface{}); ok { logger.Debugf("Found map[interface{}]interface{} value for %s", fqKey) tmp := make(map[string]interface{}) for ik, iv := range m { cik, ok := ik.(string) if !ok { panic("Non string key-entry") } tmp[cik] = iv } result[key] = getKeysRecursively(fqKey+".", v, tmp) } else if m, ok := val.(map[string]interface{}); ok { logger.Debugf("Found map[string]interface{} value for %s", fqKey) result[key] = getKeysRecursively(fqKey+".", v, m) } else { logger.Debugf("Found real value for %s setting to %T %v", fqKey, val, val) result[key] = val } } return result }
func loadBoolPtr(key string, v *viper.Viper) *bool { val := v.Get(key) if val == nil { return nil } b := v.GetBool(key) return &b }
// New is the constructor for Application struct. func New(config *viper.Viper) (*Application, error) { cookieStoreSecret := config.Get("cookie_secret").(string) app := &Application{} app.config = config app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret)) return app, nil }
func setDefaultPolicyConfigValuesWithViper(v *viper.Viper, p *PolicyDefinition) error { stmts, err := extractArray(v.Get("policy.statements")) if err != nil { return err } for i, _ := range p.Statements { vv := viper.New() if len(stmts) > i { vv.Set("statement", stmts[i]) } if !vv.IsSet("statement.actions.route-disposition") { p.Statements[i].Actions.RouteDisposition = ROUTE_DISPOSITION_NONE } } return nil }
// New is the constructor for Application struct. func New(config *viper.Viper) (*Application, error) { dsn := config.Get("dsn").(string) db, err := sqlx.Connect("mysql", dsn) if err != nil { return nil, err } cookieStoreSecret := config.Get("cookie_secret").(string) app := &Application{} app.config = config app.dsn = dsn app.db = db app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret)) return app, nil }
func SetDefaultConfigValues(v *viper.Viper, b *Bgp) error { if v == nil { v = viper.New() } defaultAfiSafi := func(typ AfiSafiType, enable bool) AfiSafi { return AfiSafi{ AfiSafiName: typ, Config: AfiSafiConfig{ AfiSafiName: typ, Enabled: enable, }, State: AfiSafiState{ AfiSafiName: typ, }, } } if !v.IsSet("global.afi-safis") { b.Global.AfiSafis = []AfiSafi{} for k, _ := range AfiSafiTypeToIntMap { b.Global.AfiSafis = append(b.Global.AfiSafis, defaultAfiSafi(k, true)) } } if b.Global.ListenConfig.Port == 0 { b.Global.ListenConfig.Port = bgp.BGP_PORT } for idx, server := range b.Global.BmpServers { if server.Config.Port == 0 { server.Config.Port = bgp.BMP_DEFAULT_PORT } b.Global.BmpServers[idx] = server } if !v.IsSet("global.mpls-label-range.min-label") { b.Global.MplsLabelRange.MinLabel = DEFAULT_MPLS_LABEL_MIN } if !v.IsSet("global.mpls-label-range.max-label") { b.Global.MplsLabelRange.MaxLabel = DEFAULT_MPLS_LABEL_MAX } // yaml is decoded as []interface{} // but toml is decoded as []map[string]interface{}. // currently, viper can't hide this difference. // handle the difference here. extractArray := func(intf interface{}) ([]interface{}, error) { if intf != nil { list, ok := intf.([]interface{}) if ok { return list, nil } l, ok := intf.([]map[string]interface{}) if !ok { return nil, fmt.Errorf("invalid configuration: neither []interface{} nor []map[string]interface{}") } list = make([]interface{}, 0, len(l)) for _, m := range l { list = append(list, m) } return list, nil } return nil, nil } list, err := extractArray(v.Get("neighbors")) if err != nil { return err } for idx, n := range b.Neighbors { vv := viper.New() if len(list) > idx { vv.Set("neighbor", list[idx]) } if !vv.IsSet("neighbor.timers.config.connect-retry") { n.Timers.Config.ConnectRetry = float64(DEFAULT_CONNECT_RETRY) } if !vv.IsSet("neighbor.timers.config.hold-time") { n.Timers.Config.HoldTime = float64(DEFAULT_HOLDTIME) } if !vv.IsSet("neighbor.timers.config.keepalive-interval") { n.Timers.Config.KeepaliveInterval = n.Timers.Config.HoldTime / 3 } if !vv.IsSet("neighbor.timers.config.idle-hold-time-after-reset") { n.Timers.Config.IdleHoldTimeAfterReset = float64(DEFAULT_IDLE_HOLDTIME_AFTER_RESET) } if !vv.IsSet("neighbor.afi-safis") { if ip := net.ParseIP(n.Config.NeighborAddress); ip.To4() != nil { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV4_UNICAST, true)} } else if ip.To16() != nil { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV6_UNICAST, true)} } else { return fmt.Errorf("invalid neighbor address: %s", n.Config.NeighborAddress) } } else { afs, err := extractArray(vv.Get("neighbor.afi-safis")) if err != nil { return err } for i, af := range n.AfiSafis { vvv := viper.New() if len(afs) > i { vvv.Set("afi-safi", afs[i]) } af.Config.AfiSafiName = af.AfiSafiName af.State.AfiSafiName = af.AfiSafiName if !vvv.IsSet("afi-safi.config") { af.Config.Enabled = true } n.AfiSafis[i] = af } } if !vv.IsSet("neighbor.config.peer-type") { if n.Config.PeerAs != b.Global.Config.As { n.Config.PeerType = PEER_TYPE_EXTERNAL } else { n.Config.PeerType = PEER_TYPE_INTERNAL } } b.Neighbors[idx] = n } for _, r := range b.RpkiServers { if r.Config.Port == 0 { r.Config.Port = bgp.RPKI_DEFAULT_PORT } } return nil }
func setDefaultNeighborConfigValuesWithViper(v *viper.Viper, n *Neighbor, asn uint32) error { if v == nil { v = viper.New() } if n.Config.LocalAs == 0 { n.Config.LocalAs = asn } if n.Config.PeerAs != n.Config.LocalAs { n.Config.PeerType = PEER_TYPE_EXTERNAL } else { n.Config.PeerType = PEER_TYPE_INTERNAL } if !v.IsSet("neighbor.timers.config.connect-retry") && n.Timers.Config.ConnectRetry == 0 { n.Timers.Config.ConnectRetry = float64(DEFAULT_CONNECT_RETRY) } if !v.IsSet("neighbor.timers.config.hold-time") && n.Timers.Config.HoldTime == 0 { n.Timers.Config.HoldTime = float64(DEFAULT_HOLDTIME) } if !v.IsSet("neighbor.timers.config.keepalive-interval") && n.Timers.Config.KeepaliveInterval == 0 { n.Timers.Config.KeepaliveInterval = n.Timers.Config.HoldTime / 3 } if !v.IsSet("neighbor.timers.config.idle-hold-time-after-reset") && n.Timers.Config.IdleHoldTimeAfterReset == 0 { n.Timers.Config.IdleHoldTimeAfterReset = float64(DEFAULT_IDLE_HOLDTIME_AFTER_RESET) } if n.Config.NeighborInterface != "" { addr, err := GetIPv6LinkLocalNeighborAddress(n.Config.NeighborInterface) if err != nil { return err } n.Config.NeighborAddress = addr } if n.Transport.Config.LocalAddress == "" { if n.Config.NeighborAddress == "" { return fmt.Errorf("no neighbor address/interface specified") } ipAddr, err := net.ResolveIPAddr("ip", n.Config.NeighborAddress) if err != nil { return err } localAddress := "0.0.0.0" if ipAddr.IP.To4() == nil { localAddress = "::" if ipAddr.Zone != "" { localAddress, err = getIPv6LinkLocalAddress(ipAddr.Zone) if err != nil { return err } } } n.Transport.Config.LocalAddress = localAddress } if len(n.AfiSafis) == 0 { if ipAddr, err := net.ResolveIPAddr("ip", n.Config.NeighborAddress); err != nil { return fmt.Errorf("invalid neighbor address: %s", n.Config.NeighborAddress) } else if ipAddr.IP.To4() != nil { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV4_UNICAST, true)} } else { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV6_UNICAST, true)} } } else { afs, err := extractArray(v.Get("neighbor.afi-safis")) if err != nil { return err } for i, af := range n.AfiSafis { vv := viper.New() if len(afs) > i { vv.Set("afi-safi", afs[i]) } af.State.AfiSafiName = af.Config.AfiSafiName if !vv.IsSet("afi-safi.config") { af.Config.Enabled = true } n.AfiSafis[i] = af } } n.State.Description = n.Config.Description n.State.AdminDown = n.Config.AdminDown if n.GracefulRestart.Config.Enabled { if !v.IsSet("neighbor.graceful-restart.config.restart-time") && n.GracefulRestart.Config.RestartTime == 0 { // RFC 4724 4. Operation // A suggested default for the Restart Time is a value less than or // equal to the HOLDTIME carried in the OPEN. n.GracefulRestart.Config.RestartTime = uint16(n.Timers.Config.HoldTime) } if !v.IsSet("neighbor.graceful-restart.config.deferral-time") && n.GracefulRestart.Config.DeferralTime == 0 { // RFC 4724 4.1. Procedures for the Restarting Speaker // The value of this timer should be large // enough, so as to provide all the peers of the Restarting Speaker with // enough time to send all the routes to the Restarting Speaker n.GracefulRestart.Config.DeferralTime = uint16(360) } } return nil }
func SetDefaultConfigValues(v *viper.Viper, b *BgpConfigSet) error { if v == nil { v = viper.New() } defaultAfiSafi := func(typ AfiSafiType, enable bool) AfiSafi { return AfiSafi{ Config: AfiSafiConfig{ AfiSafiName: typ, Enabled: enable, }, State: AfiSafiState{ AfiSafiName: typ, }, } } if b.Zebra.Config.Url == "" { b.Zebra.Config.Url = "unix:/var/run/quagga/zserv.api" } if len(b.Global.AfiSafis) == 0 { b.Global.AfiSafis = []AfiSafi{} for k, _ := range AfiSafiTypeToIntMap { b.Global.AfiSafis = append(b.Global.AfiSafis, defaultAfiSafi(k, true)) } } if b.Global.Config.Port == 0 { b.Global.Config.Port = bgp.BGP_PORT } if len(b.Global.Config.LocalAddressList) == 0 { b.Global.Config.LocalAddressList = []string{"0.0.0.0", "::"} } for idx, server := range b.BmpServers { if server.Config.Port == 0 { server.Config.Port = bmp.BMP_DEFAULT_PORT } b.BmpServers[idx] = server } if b.Global.MplsLabelRange.MinLabel == 0 { b.Global.MplsLabelRange.MinLabel = DEFAULT_MPLS_LABEL_MIN } if b.Global.MplsLabelRange.MaxLabel == 0 { b.Global.MplsLabelRange.MaxLabel = DEFAULT_MPLS_LABEL_MAX } // yaml is decoded as []interface{} // but toml is decoded as []map[string]interface{}. // currently, viper can't hide this difference. // handle the difference here. extractArray := func(intf interface{}) ([]interface{}, error) { if intf != nil { list, ok := intf.([]interface{}) if ok { return list, nil } l, ok := intf.([]map[string]interface{}) if !ok { return nil, fmt.Errorf("invalid configuration: neither []interface{} nor []map[string]interface{}") } list = make([]interface{}, 0, len(l)) for _, m := range l { list = append(list, m) } return list, nil } return nil, nil } list, err := extractArray(v.Get("neighbors")) if err != nil { return err } for idx, n := range b.Neighbors { vv := viper.New() if len(list) > idx { vv.Set("neighbor", list[idx]) } if !vv.IsSet("neighbor.timers.config.connect-retry") { n.Timers.Config.ConnectRetry = float64(DEFAULT_CONNECT_RETRY) } if !vv.IsSet("neighbor.timers.config.hold-time") { n.Timers.Config.HoldTime = float64(DEFAULT_HOLDTIME) } if !vv.IsSet("neighbor.timers.config.keepalive-interval") { n.Timers.Config.KeepaliveInterval = n.Timers.Config.HoldTime / 3 } if !vv.IsSet("neighbor.timers.config.idle-hold-time-after-reset") { n.Timers.Config.IdleHoldTimeAfterReset = float64(DEFAULT_IDLE_HOLDTIME_AFTER_RESET) } if !vv.IsSet("neighbor.transport.config.local-address") { v6 := true if ip := net.ParseIP(n.Config.NeighborAddress); ip.To4() != nil { v6 = false } if v6 { n.Transport.Config.LocalAddress = "::" } else { n.Transport.Config.LocalAddress = "0.0.0.0" } } if !vv.IsSet("neighbor.afi-safis") { if ip := net.ParseIP(n.Config.NeighborAddress); ip.To4() != nil { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV4_UNICAST, true)} } else if ip.To16() != nil { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV6_UNICAST, true)} } else { return fmt.Errorf("invalid neighbor address: %s", n.Config.NeighborAddress) } } else { afs, err := extractArray(vv.Get("neighbor.afi-safis")) if err != nil { return err } for i, af := range n.AfiSafis { vvv := viper.New() if len(afs) > i { vvv.Set("afi-safi", afs[i]) } af.State.AfiSafiName = af.Config.AfiSafiName if !vvv.IsSet("afi-safi.config") { af.Config.Enabled = true } n.AfiSafis[i] = af } } n.State.Description = n.Config.Description n.Config.Description = "" n.State.AdminDown = n.Config.AdminDown if !vv.IsSet("neighbor.config.local-as") { n.Config.LocalAs = b.Global.Config.As } if !vv.IsSet("neighbor.config.peer-type") { if n.Config.PeerAs != n.Config.LocalAs { n.Config.PeerType = PEER_TYPE_EXTERNAL } else { n.Config.PeerType = PEER_TYPE_INTERNAL } } if n.GracefulRestart.Config.Enabled { if !vv.IsSet("neighbor.graceful-restart.config.restart-time") { // RFC 4724 4. Operation // A suggested default for the Restart Time is a value less than or // equal to the HOLDTIME carried in the OPEN. n.GracefulRestart.Config.RestartTime = uint16(n.Timers.Config.HoldTime) } if !vv.IsSet("neighbor.graceful-restart.config.deferral-time") { // RFC 4724 4.1. Procedures for the Restarting Speaker // The value of this timer should be large // enough, so as to provide all the peers of the Restarting Speaker with // enough time to send all the routes to the Restarting Speaker n.GracefulRestart.Config.DeferralTime = uint16(360) } } b.Neighbors[idx] = n } for _, r := range b.RpkiServers { if r.Config.Port == 0 { r.Config.Port = rtr.RPKI_DEFAULT_PORT } } return nil }