Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 4c1adb6

Browse files
authored
rename client_id in pool to object_id (#2673)
* rename client_id in pool to object_id * fix tests * print out the content body when receiving an error response in the agent * fix test * Apply suggestions from code review * Update src/ApiService/ApiService/Functions/AgentRegistration.cs * format * cleanup * format * address pr comment
1 parent 3cf09c6 commit 4c1adb6

File tree

15 files changed

+120
-79
lines changed

15 files changed

+120
-79
lines changed

src/ApiService/ApiService/Functions/AgentRegistration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ private async Async.Task<AgentRegistrationResponse> CreateRegistrationResponse(S
7373
var baseAddress = _context.Creds.GetInstanceUrl();
7474
var eventsUrl = new Uri(baseAddress, "/api/agents/events");
7575
var commandsUrl = new Uri(baseAddress, "/api/agents/commands");
76-
7776
var workQueue = await _context.Queue.GetQueueSas(
7877
_context.PoolOperations.GetPoolQueue(pool.PoolId),
7978
StorageType.Corpus,

src/ApiService/ApiService/Functions/Pool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private async Task<HttpResponseData> Post(HttpRequestData req) {
6767
Errors: new string[] { "pool with that name already exists" }),
6868
"PoolCreate");
6969
}
70-
var newPool = await _context.PoolOperations.Create(name: create.Name, os: create.Os, architecture: create.Arch, managed: create.Managed, clientId: create.ClientId);
70+
var newPool = await _context.PoolOperations.Create(name: create.Name, os: create.Os, architecture: create.Arch, managed: create.Managed, objectId: create.ObjectId);
7171
return await RequestHandling.Ok(req, await Populate(PoolToPoolResponse(newPool), true));
7272
}
7373

@@ -106,7 +106,7 @@ private static PoolGetResult PoolToPoolResponse(Service.Pool p)
106106
PoolId: p.PoolId,
107107
Os: p.Os,
108108
State: p.State,
109-
ClientId: p.ClientId,
109+
ObjectId: p.ObjectId,
110110
Managed: p.Managed,
111111
Arch: p.Arch,
112112
Nodes: p.Nodes,

src/ApiService/ApiService/OneFuzzTypes/Model.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ public record Pool(
645645
bool Managed,
646646
Architecture Arch,
647647
PoolState State,
648-
Guid? ClientId = null
648+
Guid? ObjectId = null
649649
) : StatefulEntityBase<PoolState>(State) {
650650
public List<Node>? Nodes { get; set; }
651651
public AgentConfig? Config { get; set; }

src/ApiService/ApiService/OneFuzzTypes/Requests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public record PoolCreate(
263263
[property: Required] Os Os,
264264
[property: Required] Architecture Arch,
265265
[property: Required] bool Managed,
266-
Guid? ClientId = null
266+
Guid? ObjectId = null
267267
) : BaseRequest;
268268

269269
public record WebhookCreate(

src/ApiService/ApiService/OneFuzzTypes/Responses.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public record PoolGetResult(
114114
bool Managed,
115115
Architecture Arch,
116116
PoolState State,
117-
Guid? ClientId,
117+
Guid? ObjectId,
118118
List<Node>? Nodes,
119119
AgentConfig? Config,
120120
List<WorkSetSummary>? WorkQueue,

src/ApiService/ApiService/ServiceConfiguration.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public interface IServiceConfig {
5050
// multiple instances to run against the same storage account, which
5151
// is useful for things like integration testing.
5252
public string OneFuzzStoragePrefix { get; }
53+
54+
public Uri OneFuzzBaseAddress { get; }
5355
}
5456

5557
public class ServiceConfiguration : IServiceConfig {
@@ -134,4 +136,12 @@ public string OneFuzzVersion {
134136

135137
public string OneFuzzNodeDisposalStrategy { get => GetEnv("ONEFUZZ_NODE_DISPOSAL_STRATEGY") ?? "scale_in"; }
136138
public string OneFuzzStoragePrefix => ""; // in production we never prefix the tables
139+
140+
public Uri OneFuzzBaseAddress {
141+
get {
142+
var hostName = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
143+
var scheme = Environment.GetEnvironmentVariable("HTTPS") != null ? "https" : "http";
144+
return new Uri($"{scheme}://{hostName}");
145+
}
146+
}
137147
}

src/ApiService/ApiService/onefuzzlib/EndpointAuthorization.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ public virtual async Async.Task<HttpResponseData> CallIf(HttpRequestData req, Fu
4646
}
4747

4848
var token = tokenResult.OkV.UserInfo;
49-
if (await IsUser(tokenResult.OkV)) {
49+
50+
var (isAgent, reason) = await IsAgent(tokenResult.OkV);
51+
52+
if (!isAgent) {
5053
if (!allowUser) {
51-
return await Reject(req, token);
54+
return await Reject(req, token, "endpoint not allowed for users");
5255
}
5356

5457
var access = await CheckAccess(req);
@@ -57,26 +60,24 @@ public virtual async Async.Task<HttpResponseData> CallIf(HttpRequestData req, Fu
5760
}
5861
}
5962

60-
if (await IsAgent(tokenResult.OkV) && !allowAgent) {
61-
return await Reject(req, token);
63+
64+
if (isAgent && !allowAgent) {
65+
return await Reject(req, token, reason);
6266
}
6367

6468
return await method(req);
6569
}
6670

67-
public async Async.Task<bool> IsUser(UserAuthInfo tokenData) {
68-
return !await IsAgent(tokenData);
69-
}
7071

71-
public async Async.Task<HttpResponseData> Reject(HttpRequestData req, UserInfo token) {
72+
public async Async.Task<HttpResponseData> Reject(HttpRequestData req, UserInfo token, String? reason = null) {
7273
var body = await req.ReadAsStringAsync();
73-
_log.Error($"reject token. url:{req.Url:Tag:Url} token:{token:Tag:Token} body:{body:Tag:Body}");
74+
_log.Error($"reject token. reason:{reason} url:{req.Url:Tag:Url} token:{token:Tag:Token} body:{body:Tag:Body}");
7475

7576
return await _context.RequestHandling.NotOk(
7677
req,
7778
new Error(
7879
ErrorCode.UNAUTHORIZED,
79-
new string[] { "Unrecognized agent" }
80+
new string[] { reason ?? "Unrecognized agent" }
8081
),
8182
"token verification",
8283
HttpStatusCode.Unauthorized
@@ -186,34 +187,35 @@ private GroupMembershipChecker CreateGroupMembershipChecker(InstanceConfig confi
186187
return null;
187188
}
188189

189-
public async Async.Task<bool> IsAgent(UserAuthInfo authInfo) {
190+
191+
public async Async.Task<(bool, string)> IsAgent(UserAuthInfo authInfo) {
190192
if (!AgentRoles.Overlaps(authInfo.Roles)) {
191-
return false;
193+
return (false, "no agent role");
192194
}
193195

194196
var tokenData = authInfo.UserInfo;
195197

196198
if (tokenData.ObjectId != null) {
197199
var scalesets = _context.ScalesetOperations.GetByObjectId(tokenData.ObjectId.Value);
198200
if (await scalesets.AnyAsync()) {
199-
return true;
201+
return (true, string.Empty);
200202
}
201203

202204
var principalId = await _context.Creds.GetScalesetPrincipalId();
203205
if (principalId == tokenData.ObjectId) {
204-
return true;
206+
return (true, string.Empty);
205207
}
206208
}
207209

208-
if (!tokenData.ApplicationId.HasValue) {
209-
return false;
210+
if (!tokenData.ObjectId.HasValue) {
211+
return (false, "no object id in token");
210212
}
211213

212-
var pools = _context.PoolOperations.GetByClientId(tokenData.ApplicationId.Value);
214+
var pools = _context.PoolOperations.GetByObjectId(tokenData.ObjectId.Value);
213215
if (await pools.AnyAsync()) {
214-
return true;
216+
return (true, string.Empty);
215217
}
216218

217-
return false;
219+
return (false, "no matching scaleset or pool");
218220
}
219221
}

src/ApiService/ApiService/onefuzzlib/PoolOperations.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ public interface IPoolOperations : IStatefulOrm<Pool, PoolState> {
66
Async.Task<OneFuzzResult<Pool>> GetByName(PoolName poolName);
77
Async.Task<OneFuzzResult<Pool>> GetById(Guid poolId);
88
Task<bool> ScheduleWorkset(Pool pool, WorkSet workSet);
9-
IAsyncEnumerable<Pool> GetByClientId(Guid clientId);
9+
IAsyncEnumerable<Pool> GetByObjectId(Guid objectId);
1010
string GetPoolQueue(Guid poolId);
1111
Async.Task<List<ScalesetSummary>> GetScalesetSummary(PoolName name);
1212
Async.Task<List<WorkSetSummary>> GetWorkQueue(Guid poolId, PoolState state);
1313
IAsyncEnumerable<Pool> SearchStates(IEnumerable<PoolState> states);
1414
Async.Task<Pool> SetShutdown(Pool pool, bool Now);
1515

16-
Async.Task<Pool> Create(PoolName name, Os os, Architecture architecture, bool managed, Guid? clientId = null);
16+
Async.Task<Pool> Create(PoolName name, Os os, Architecture architecture, bool managed, Guid? objectId = null);
1717
new Async.Task Delete(Pool pool);
1818

1919
// state transitions:
@@ -32,15 +32,15 @@ public PoolOperations(ILogTracer log, IOnefuzzContext context)
3232

3333
}
3434

35-
public async Async.Task<Pool> Create(PoolName name, Os os, Architecture architecture, bool managed, Guid? clientId = null) {
35+
public async Async.Task<Pool> Create(PoolName name, Os os, Architecture architecture, bool managed, Guid? objectId = null) {
3636
var newPool = new Service.Pool(
3737
PoolId: Guid.NewGuid(),
3838
State: PoolState.Init,
3939
Name: name,
4040
Os: os,
4141
Managed: managed,
4242
Arch: architecture,
43-
ClientId: clientId);
43+
ObjectId: objectId);
4444

4545
var r = await Insert(newPool);
4646
if (!r.IsOk) {
@@ -87,8 +87,8 @@ public async Task<bool> ScheduleWorkset(Pool pool, WorkSet workSet) {
8787
return await _context.Queue.QueueObject(GetPoolQueue(pool.PoolId), workSet, StorageType.Corpus);
8888
}
8989

90-
public IAsyncEnumerable<Pool> GetByClientId(Guid clientId) {
91-
return QueryAsync(filter: $"client_id eq '{clientId}'");
90+
public IAsyncEnumerable<Pool> GetByObjectId(Guid objectId) {
91+
return QueryAsync(filter: $"object_id eq '{objectId}'");
9292
}
9393

9494
public string GetPoolQueue(Guid poolId)

src/ApiService/IntegrationTests/Fakes/TestServiceConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ public TestServiceConfiguration(string tablePrefix) {
6464

6565
public string? OneFuzzAllowOutdatedAgent => throw new NotImplementedException();
6666
public string? AppConfigurationEndpoint => throw new NotImplementedException();
67+
public Uri OneFuzzBaseAddress { get => new Uri("http://test"); }
6768
public string? AppConfigurationConnectionString => throw new NotImplementedException();
6869
}

src/agent/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
target
2-
.agent-run
2+
.agent-run

0 commit comments

Comments
 (0)