@@ -7,59 +7,49 @@ package apiv1
7
7
import (
8
8
"context"
9
9
10
+ connect "github.com/bufbuild/connect-go"
10
11
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
12
+ "github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
11
13
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
12
14
v1 "github.com/gitpod-io/gitpod/public-api/v1"
15
+ "github.com/gitpod-io/gitpod/public-api/v1/v1connect"
13
16
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
14
17
"github.com/relvacode/iso8601"
15
18
"google.golang.org/grpc"
16
19
"google.golang.org/grpc/codes"
17
- "google.golang.org/grpc/metadata"
18
20
"google.golang.org/grpc/status"
19
21
"google.golang.org/protobuf/types/known/timestamppb"
20
22
)
21
23
22
24
func NewWorkspaceService (serverConnPool proxy.ServerConnectionPool ) * WorkspaceService {
23
25
return & WorkspaceService {
24
- connectionPool : serverConnPool ,
25
- UnimplementedWorkspacesServiceServer : & v1.UnimplementedWorkspacesServiceServer {},
26
+ connectionPool : serverConnPool ,
26
27
}
27
28
}
28
29
29
30
type WorkspaceService struct {
30
31
connectionPool proxy.ServerConnectionPool
31
32
32
- * v1. UnimplementedWorkspacesServiceServer
33
+ v1connect. WorkspacesServiceHandler
33
34
}
34
35
35
- func (w * WorkspaceService ) GetWorkspace (ctx context.Context , r * v1.GetWorkspaceRequest ) (* v1.GetWorkspaceResponse , error ) {
36
+ func (s * WorkspaceService ) GetWorkspace (ctx context.Context , req * connect.Request [v1.GetWorkspaceRequest ]) (* connect.Response [v1.GetWorkspaceResponse ], error ) {
37
+ token := auth .TokenFromContext (ctx )
36
38
logger := ctxlogrus .Extract (ctx )
37
- token , err := bearerTokenFromContext (ctx )
38
- if err != nil {
39
- return nil , err
40
- }
41
39
42
- server , err := w .connectionPool .Get (ctx , token )
40
+ server , err := s .connectionPool .Get (ctx , token )
43
41
if err != nil {
44
42
logger .WithError (err ).Error ("Failed to get connection to server." )
45
- return nil , status . Error ( codes . Internal , "failed to establish connection to downstream services" )
43
+ return nil , connect . NewError ( connect . CodeInternal , err )
46
44
}
47
45
48
- workspace , err := server .GetWorkspace (ctx , r .GetWorkspaceId ())
46
+ workspace , err := server .GetWorkspace (ctx , req . Msg .GetWorkspaceId ())
49
47
if err != nil {
50
48
logger .WithError (err ).Error ("Failed to get workspace." )
51
- converted := proxy .ConvertError (err )
52
- switch status .Code (converted ) {
53
- case codes .PermissionDenied :
54
- return nil , status .Error (codes .PermissionDenied , "insufficient permission to access workspace" )
55
- case codes .NotFound :
56
- return nil , status .Error (codes .NotFound , "workspace does not exist" )
57
- default :
58
- return nil , status .Error (codes .Internal , "unable to retrieve workspace" )
59
- }
49
+ return nil , proxy .ConvertError (err )
60
50
}
61
51
62
- return & v1.GetWorkspaceResponse {
52
+ return connect . NewResponse ( & v1.GetWorkspaceResponse {
63
53
Result : & v1.Workspace {
64
54
WorkspaceId : workspace .Workspace .ID ,
65
55
OwnerId : workspace .Workspace .OwnerID ,
@@ -73,72 +63,20 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR
73
63
},
74
64
Description : workspace .Workspace .Description ,
75
65
},
76
- }, nil
77
- }
78
-
79
- func (w * WorkspaceService ) GetOwnerToken (ctx context.Context , r * v1.GetOwnerTokenRequest ) (* v1.GetOwnerTokenResponse , error ) {
80
- logger := ctxlogrus .Extract (ctx )
81
- token , err := bearerTokenFromContext (ctx )
82
- if err != nil {
83
- return nil , err
84
- }
85
-
86
- server , err := w .connectionPool .Get (ctx , token )
87
- if err != nil {
88
- logger .WithError (err ).Error ("Failed to get connection to server." )
89
- return nil , status .Error (codes .Internal , "failed to establish connection to downstream services" )
90
- }
91
-
92
- ownerToken , err := server .GetOwnerToken (ctx , r .GetWorkspaceId ())
93
-
94
- if err != nil {
95
- logger .WithError (err ).Error ("Failed to get owner token." )
96
- converted := proxy .ConvertError (err )
97
- switch status .Code (converted ) {
98
- case codes .PermissionDenied :
99
- return nil , status .Error (codes .PermissionDenied , "insufficient permission to retrieve ownertoken" )
100
- case codes .NotFound :
101
- return nil , status .Error (codes .NotFound , "workspace does not exist" )
102
- default :
103
- return nil , status .Error (codes .Internal , "unable to retrieve owner token" )
104
- }
105
- }
106
-
107
- return & v1.GetOwnerTokenResponse {Token : ownerToken }, nil
66
+ }), nil
108
67
}
109
68
110
- func bearerTokenFromContext (ctx context.Context ) (string , error ) {
111
- md , ok := metadata .FromIncomingContext (ctx )
112
- if ! ok {
113
- return "" , status .Error (codes .Unauthenticated , "no credentials provided" )
114
- }
115
-
116
- values := md .Get ("authorization" )
117
- if len (values ) == 0 {
118
- return "" , status .Error (codes .Unauthenticated , "no authorization header specified" )
119
- }
120
- if len (values ) > 1 {
121
- return "" , status .Error (codes .Unauthenticated , "more than one authorization header specified, exactly one is required" )
122
- }
123
-
124
- token := values [0 ]
125
- return token , nil
126
- }
127
-
128
- func (w * WorkspaceService ) ListWorkspaces (ctx context.Context , req * v1.ListWorkspacesRequest ) (* v1.ListWorkspacesResponse , error ) {
69
+ func (s * WorkspaceService ) ListWorkspaces (ctx context.Context , req * connect.Request [v1.ListWorkspacesRequest ]) (* connect.Response [v1.ListWorkspacesResponse ], error ) {
129
70
logger := ctxlogrus .Extract (ctx )
130
- token , err := bearerTokenFromContext (ctx )
131
- if err != nil {
132
- return nil , err
133
- }
71
+ token := auth .TokenFromContext (ctx )
134
72
135
- server , err := w .connectionPool .Get (ctx , token )
73
+ server , err := s .connectionPool .Get (ctx , token )
136
74
if err != nil {
137
75
logger .WithError (err ).Error ("Failed to get connection to server." )
138
76
return nil , status .Error (codes .Internal , "failed to establish connection to downstream services" )
139
77
}
140
78
141
- limit , err := getLimitFromPagination (req .Pagination )
79
+ limit , err := getLimitFromPagination (req .Msg . GetPagination () )
142
80
if err != nil {
143
81
// getLimitFromPagination returns gRPC errors
144
82
return nil , err
@@ -161,9 +99,11 @@ func (w *WorkspaceService) ListWorkspaces(ctx context.Context, req *v1.ListWorks
161
99
res = append (res , workspaceAndInstance )
162
100
}
163
101
164
- return & v1.ListWorkspacesResponse {
165
- Result : res ,
166
- }, nil
102
+ return connect .NewResponse (
103
+ & v1.ListWorkspacesResponse {
104
+ Result : res ,
105
+ },
106
+ ), nil
167
107
}
168
108
169
109
func getLimitFromPagination (pagination * v1.Pagination ) (int , error ) {
0 commit comments