func (*tagSuite) TestParseTag(c *gc.C) { for i, test := range parseTagTests { c.Logf("test %d: %q expectKind %q", i, test.tag, test.expectKind) kind, id, err := names.ParseTag(test.tag, test.expectKind) if test.resultErr != "" { c.Assert(err, gc.ErrorMatches, test.resultErr) c.Assert(kind, gc.Equals, "") c.Assert(id, gc.Equals, "") // If the tag has a valid kind which matches the // expected kind, test that using an empty // expectKind does not change the error message. if tagKind, err := names.TagKind(test.tag); err == nil && tagKind == test.expectKind { kind, id, err := names.ParseTag(test.tag, "") c.Assert(err, gc.ErrorMatches, test.resultErr) c.Assert(kind, gc.Equals, "") c.Assert(id, gc.Equals, "") } } else { c.Assert(err, gc.IsNil) c.Assert(id, gc.Equals, test.resultId) if test.expectKind != "" { c.Assert(kind, gc.Equals, test.expectKind) } else { expectKind, err := names.TagKind(test.tag) c.Assert(err, gc.IsNil) c.Assert(kind, gc.Equals, expectKind) } // Check that it's reversible. if f := makeTag[kind]; f != nil { reversed := f(id) c.Assert(reversed, gc.Equals, test.tag) } // Check that it parses ok without an expectKind. kind1, id1, err1 := names.ParseTag(test.tag, "") c.Assert(err1, gc.IsNil) c.Assert(kind1, gc.Equals, test.expectKind) c.Assert(id1, gc.Equals, id) } } }
func (*tagSuite) TestTagKind(c *gc.C) { for i, test := range tagKindTests { c.Logf("test %d: %q -> %q", i, test.tag, test.kind) kind, err := names.TagKind(test.tag) if test.err == "" { c.Assert(test.kind, gc.Equals, kind) c.Assert(err, gc.IsNil) } else { c.Assert(kind, gc.Equals, "") c.Assert(err, gc.ErrorMatches, test.err) } } }
func newRsyslogConfigHandler(st *apirsyslog.State, mode RsyslogMode, tag, namespace string, stateServerAddrs []string) (*RsyslogConfigHandler, error) { var syslogConfig *syslog.SyslogConfig if mode == RsyslogModeAccumulate { syslogConfig = syslog.NewAccumulateConfig( tag, logDir, 0, namespace, ) } else { syslogConfig = syslog.NewForwardConfig( tag, logDir, 0, namespace, stateServerAddrs, ) } // Historically only machine-0 includes the namespace in the log // dir/file; for backwards compatibility we continue the tradition. if tag != "machine-0" { namespace = "" } kind, err := names.TagKind(tag) if err != nil { return nil, err } if kind == names.MachineTagKind { if namespace == "" { syslogConfig.ConfigFileName = "25-juju.conf" } else { syslogConfig.ConfigFileName = fmt.Sprintf("25-juju-%s.conf", namespace) } } else { syslogConfig.ConfigFileName = fmt.Sprintf("26-juju-%s.conf", tag) } syslogConfig.ConfigDir = rsyslogConfDir syslogConfig.LogDir = logDir if namespace != "" { syslogConfig.LogDir += "-" + namespace } return &RsyslogConfigHandler{ st: st, mode: mode, syslogConfig: syslogConfig, }, nil }
// Login logs in with the provided credentials. // All subsequent requests on the connection will // act as the authenticated user. func (a *srvAdmin) Login(c params.Creds) (params.LoginResult, error) { a.mu.Lock() defer a.mu.Unlock() if a.loggedIn { // This can only happen if Login is called concurrently. return params.LoginResult{}, errAlreadyLoggedIn } // Users are not rate limited, all other entities are if kind, err := names.TagKind(c.AuthTag); err != nil || kind != names.UserTagKind { if !a.limiter.Acquire() { logger.Debugf("rate limiting, try again later") return params.LoginResult{}, common.ErrTryAgain } defer a.limiter.Release() } entity, err := doCheckCreds(a.root.srv.state, c) if err != nil { return params.LoginResult{}, err } if a.reqNotifier != nil { a.reqNotifier.login(entity.Tag()) } // We have authenticated the user; now choose an appropriate API // to serve to them. // TODO: consider switching the new root based on who is logging in newRoot := newSrvRoot(a.root, entity) if err := a.startPingerIfAgent(newRoot, entity); err != nil { return params.LoginResult{}, err } // Fetch the API server addresses from state. hostPorts, err := a.root.srv.state.APIHostPorts() if err != nil { return params.LoginResult{}, err } logger.Debugf("hostPorts: %v", hostPorts) a.root.rpcConn.Serve(newRoot, serverError) return params.LoginResult{hostPorts}, nil }