1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/lib/controller/railsproxy"
17 "git.arvados.org/arvados.git/lib/controller/rpc"
18 "git.arvados.org/arvados.git/sdk/go/arvados"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/httpserver"
21 "github.com/hashicorp/yamux"
22 "github.com/sirupsen/logrus"
25 type railsProxy = rpc.Conn
28 cluster *arvados.Cluster
29 *railsProxy // handles API methods that aren't defined on Conn itself
30 vocabularyCache *arvados.Vocabulary
31 vocabularyFileModTime time.Time
32 lastVocabularyRefreshCheck time.Time
33 lastVocabularyError error
35 gwTunnels map[string]*yamux.Session
36 gwTunnelsLock sync.Mutex
37 activeUsers map[string]bool
38 activeUsersLock sync.Mutex
39 activeUsersReset time.Time
42 func NewConn(cluster *arvados.Cluster) *Conn {
43 railsProxy := railsproxy.NewConn(cluster)
44 railsProxy.RedactHostInErrors = true
47 railsProxy: railsProxy,
49 conn.loginController = chooseLoginController(cluster, &conn)
53 func (conn *Conn) checkProperties(ctx context.Context, properties interface{}) error {
54 if properties == nil {
57 var props map[string]interface{}
58 switch properties := properties.(type) {
60 err := json.Unmarshal([]byte(properties), &props)
64 case map[string]interface{}:
67 return fmt.Errorf("unexpected properties type %T", properties)
69 voc, err := conn.VocabularyGet(ctx)
73 err = voc.Check(props)
75 return httpErrorf(http.StatusBadRequest, voc.Check(props).Error())
80 func (conn *Conn) maybeRefreshVocabularyCache(logger logrus.FieldLogger) error {
81 if conn.lastVocabularyRefreshCheck.Add(time.Second).After(time.Now()) {
82 // Throttle the access to disk to at most once per second.
85 conn.lastVocabularyRefreshCheck = time.Now()
86 fi, err := os.Stat(conn.cluster.API.VocabularyPath)
88 err = fmt.Errorf("couldn't stat vocabulary file %q: %v", conn.cluster.API.VocabularyPath, err)
89 conn.lastVocabularyError = err
92 if fi.ModTime().After(conn.vocabularyFileModTime) {
93 err = conn.loadVocabularyFile()
95 conn.lastVocabularyError = err
98 conn.vocabularyFileModTime = fi.ModTime()
99 conn.lastVocabularyError = nil
100 logger.Info("vocabulary file reloaded successfully")
105 func (conn *Conn) loadVocabularyFile() error {
106 vf, err := os.ReadFile(conn.cluster.API.VocabularyPath)
108 return fmt.Errorf("while reading the vocabulary file: %v", err)
110 mk := make([]string, 0, len(conn.cluster.Collections.ManagedProperties))
111 for k := range conn.cluster.Collections.ManagedProperties {
114 voc, err := arvados.NewVocabulary(vf, mk)
116 return fmt.Errorf("while loading vocabulary file %q: %s", conn.cluster.API.VocabularyPath, err)
118 conn.vocabularyCache = voc
122 // LastVocabularyError returns the last error encountered while loading the
124 // Implements health.Func
125 func (conn *Conn) LastVocabularyError() error {
126 conn.maybeRefreshVocabularyCache(ctxlog.FromContext(context.Background()))
127 return conn.lastVocabularyError
130 // VocabularyGet refreshes the vocabulary cache if necessary and returns it.
131 func (conn *Conn) VocabularyGet(ctx context.Context) (arvados.Vocabulary, error) {
132 if conn.cluster.API.VocabularyPath == "" {
133 return arvados.Vocabulary{
134 Tags: map[string]arvados.VocabularyTag{},
137 logger := ctxlog.FromContext(ctx)
138 if conn.vocabularyCache == nil {
139 // Initial load of vocabulary file.
140 err := conn.loadVocabularyFile()
142 logger.WithError(err).Error("error loading vocabulary file")
143 return arvados.Vocabulary{}, err
146 err := conn.maybeRefreshVocabularyCache(logger)
148 logger.WithError(err).Error("error reloading vocabulary file - ignoring")
150 return *conn.vocabularyCache, nil
153 // Logout handles the logout of conn giving to the appropriate loginController
154 func (conn *Conn) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
155 return conn.loginController.Logout(ctx, opts)
158 // Login handles the login of conn giving to the appropriate loginController
159 func (conn *Conn) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
160 return conn.loginController.Login(ctx, opts)
163 // UserAuthenticate handles the User Authentication of conn giving to the appropriate loginController
164 func (conn *Conn) UserAuthenticate(ctx context.Context, opts arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
165 return conn.loginController.UserAuthenticate(ctx, opts)
168 func httpErrorf(code int, format string, args ...interface{}) error {
169 return httpserver.ErrorWithStatus(fmt.Errorf(format, args...), code)