go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/internal/buildsource/rawpresentation/html.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rawpresentation
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"go.chromium.org/luci/common/data/stringset"
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/grpc/prpc"
    24  	"go.chromium.org/luci/hardcoded/chromeinfra"
    25  	logdog "go.chromium.org/luci/logdog/api/endpoints/coordinator/logs/v1"
    26  	"go.chromium.org/luci/logdog/client/coordinator"
    27  	"go.chromium.org/luci/server/auth"
    28  )
    29  
    30  // acceptableLogdogHosts is the (hard-coded) list of accepted logdog hosts.
    31  var acceptableLogdogHosts = stringset.NewFromSlice(
    32  	chromeinfra.LogDogHost,
    33  	chromeinfra.LogDogHostAppSpot,
    34  	chromeinfra.LogDogDevHost,
    35  )
    36  
    37  func resolveHost(host string) (string, error) {
    38  	if host == "" {
    39  		host = DefaultLogDogHost
    40  	}
    41  	if acceptableLogdogHosts.Has(host) {
    42  		return host, nil
    43  	}
    44  	return "", errors.Reason("host %q is not in allowed list", host).Err()
    45  }
    46  
    47  var fakeLogKey = "holds a logdog.LogsClient"
    48  
    49  // InjectFakeLogdogClient adds the given logdog.LogsClient to the context.
    50  //
    51  // You can obtain a fake logs client from
    52  //
    53  //	go.chromium.org/luci/logdog/api/endpoints/coordinator/logs/v1/fakelogs
    54  //
    55  // Injecting a nil logs client will panic.
    56  func InjectFakeLogdogClient(c context.Context, client logdog.LogsClient) context.Context {
    57  	if client == nil {
    58  		panic("injecting nil logs client")
    59  	}
    60  	return context.WithValue(c, &fakeLogKey, client)
    61  }
    62  
    63  // NewClient generates a new LogDog client that issues requests on behalf of the
    64  // current user.
    65  func NewClient(c context.Context, host string) (*coordinator.Client, error) {
    66  	if client, _ := c.Value(&fakeLogKey).(logdog.LogsClient); client != nil {
    67  		return &coordinator.Client{
    68  			C:    client,
    69  			Host: "example.com",
    70  		}, nil
    71  	}
    72  
    73  	var err error
    74  	if host, err = resolveHost(host); err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	// Initialize the LogDog client authentication.
    79  	t, err := auth.GetRPCTransport(c, auth.AsUser)
    80  	if err != nil {
    81  		return nil, errors.New("failed to get transport for LogDog server")
    82  	}
    83  
    84  	// Setup our LogDog client.
    85  	return coordinator.NewClient(&prpc.Client{
    86  		C: &http.Client{
    87  			Transport: t,
    88  		},
    89  		Host: host,
    90  	}), nil
    91  }