Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button for downloading the application.yml config file for agent #218

Merged
merged 11 commits into from
Jan 17, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -214,6 +219,53 @@ public Result<AgentUser> getAgentInfo(@CurrentSecurityContext SysUser requestor,
}
}

/**
* Fetch the agent application.yml config, so the user don't have to edit it by themselves.
* Authenticated USER: all
* Data access: For only the user that is the agent's TEAM admin or creator will be downloaded the agent config file with specific data
*/
@GetMapping("/api/agent/downloadAgentConfigFile/{agentId}")
public Result downloadAgentConfigFile(@CurrentSecurityContext SysUser requestor,
@PathVariable(value = "agentId") String agentId,
HttpServletResponse response) throws IOException {
if (!agentManageService.checkAgentAuthorization(requestor, agentId)) {
return Result.error(HttpStatus.UNAUTHORIZED.value(), "Authentication failed");
zhou9584 marked this conversation as resolved.
Show resolved Hide resolved
}
File agentConfigFile = agentManageService.generateAgentConfigFile(agentId);
if (agentConfigFile == null) {
return Result.error(HttpStatus.BAD_REQUEST.value(), "The file was not downloaded");
}

ServletOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(agentConfigFile);
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename=" + agentConfigFile.getName());
out = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
zhou9584 marked this conversation as resolved.
Show resolved Hide resolved
return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Internal server error");
} finally {
response.flushBuffer();
try {
out.close();
in.close();
agentConfigFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}

return Result.ok();
}

/**
* Authenticated USER: all
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.microsoft.hydralab.center.repository.AgentUserRepository;
import com.microsoft.hydralab.center.util.SecretGenerator;
import com.microsoft.hydralab.common.util.Const;
import com.microsoft.hydralab.common.entity.center.AgentUser;
import com.microsoft.hydralab.common.entity.center.SysUser;
import com.microsoft.hydralab.common.entity.common.CriteriaType;
Expand All @@ -14,6 +13,9 @@
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -128,4 +130,36 @@ public void updateAgentTeam(String teamId, String teamName){

agentUserRepository.saveAll(agents);
}

public File generateAgentConfigFile(String agentId) {
AgentUser agentUser = getAgent(agentId);
if (agentUser != null) {
try {
File agentConfigFile = File.createTempFile(
"application",
".yml",
new File(".\\"));

FileWriter fileWriter = new FileWriter(agentConfigFile.getAbsolutePath());
fileWriter.write("app:\n" +
" # register to Hydra Lab Center\n" +
" registry:\n" +
" # The server hostname:port of Hydra Lab Center. If nginx enabled, switch to port of nginx\n" +
" server: 'localhost:9886'\n" +
" # The Agent info registered in Hydra Lab Center, for instance if it's running on localhost, the URL would be: http://localhost:9886/portal/index.html#/auth\n" +
" name: " + agentUser.getName() + "\n" +
" id: " + agentUser.getId() + "\n" +
" secret: " + agentUser.getSecret() + "\n" +
" # Agent Type {1 : 1*WINDOWS + n*ANDROIDS , 2 : 1*WINDOWS+1*ANDROID , 3 : iOS}\n" +
" agent-type: 1");
fileWriter.flush();
zhou9584 marked this conversation as resolved.
Show resolved Hide resolved
fileWriter.close();

return agentConfigFile;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
43 changes: 43 additions & 0 deletions react/src/component/AuthView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export default class AuthView extends BaseView {
{t.mailAddress}
</TableCell>
<TableCell id={t.id} align="center">
<IconButton onClick={() => this.downloadAgentConfigFile(t.id)}>
<span className="material-icons-outlined">download_for_offline</span>
</IconButton>
<IconButton onClick={() => this.getAgentInfo(t.id)}>
<span className="material-icons-outlined">info</span>
</IconButton>
Expand Down Expand Up @@ -554,6 +557,46 @@ export default class AuthView extends BaseView {
}).catch(this.snackBarError)
}

downloadAgentConfigFile(agentId) {
axios({
url: `/api/agent/downloadAgentConfigFile/${agentId}`,
method: 'GET',
responseType: 'blob'
}).then((res) => {
if (res.data.type.includes('application/json')) {
let reader = new FileReader()
reader.onload = function () {
let result = JSON.parse(reader.result)
if (result.code !== 200) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "error",
snackbarMessage: "The file could not be downloaded"
})
}
}
reader.readAsText(res.data)
} else {
const href = URL.createObjectURL(res.data);
const link = document.createElement('a');
link.href = href;
link.setAttribute('download', 'application.yml');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);

if (res.data.code === 200) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "success",
snackbarMessage: "Agent config file downloaded"
})
}
}
}).catch(this.snackBarError);
}

componentDidMount() {
this.getUserInfo();
this.refreshAgentList()
Expand Down