Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ public class EventTypes {
public static final String EVENT_DELETE_SECRET_API_KEY = "DELETE.USER.KEY";
public static final String API_KEY_ACCESS_UPDATE = "API.KEY.ACCESS.UPDATE";

// MCP (Model Context Protocol) server tokens
public static final String EVENT_MCP_TOKEN_CREATE = "MCP.TOKEN.CREATE";
public static final String EVENT_MCP_TOKEN_REVOKE = "MCP.TOKEN.REVOKE";

// Template Events
public static final String EVENT_TEMPLATE_CREATE = "TEMPLATE.CREATE";
public static final String EVENT_TEMPLATE_DELETE = "TEMPLATE.DELETE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.cloud.domain.Domain;
import com.cloud.exception.CloudAuthenticationException;
import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.user.UserAccount;

public interface ApiServerService {
Expand All @@ -40,6 +41,8 @@ public ResponseObject loginUser(HttpSession session, String username, String pas

public boolean verifyUser(Long userId);

boolean verifyApiKeyAccessAllowed(User user, Account account);

public String getSerializedApiError(int errorCode, String errorText, Map<String, Object[]> apiCommandParams, String responseType);

public String getSerializedApiError(ServerApiException ex, Map<String, Object[]> apiCommandParams, String responseType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.mcp;

import java.util.Date;

import javax.inject.Inject;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.McpTokenResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.mcp.McpTokenService;

@APICommand(name = "createMcpToken",
description = "Creates an access token for the MCP (Model Context Protocol) server, owned by the calling user. " +
"The token is returned only by this call and cannot be retrieved later.",
responseObject = McpTokenResponse.class,
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = true,
authorized = {RoleType.User, RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin},
since = "24.0.0")
public class CreateMcpTokenCmd extends BaseCmd {
@Inject
private McpTokenService mcpTokenService;

@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "Name of the token.")
private String name;

@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, length = 1024, description = "Description of the token.")
private String description;

@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "Expiration date of the token. If not informed, the token does not expire. " +
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
private Date endDate;

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public Date getEndDate() {
return endDate;
}

@Override
public long getEntityOwnerId() {
return CallContext.current().getCallingAccountId();
}

@Override
public void execute() {
McpTokenResponse response = mcpTokenService.createToken(this);
response.setResponseName(getCommandName());
setResponseObject(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.mcp;

import javax.inject.Inject;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.ACL;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.McpTokenResponse;
import org.apache.cloudstack.api.response.UserResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.mcp.McpTokenService;

@APICommand(name = "listMcpTokens",
description = "Lists the access tokens for the MCP (Model Context Protocol) server of a user. The tokens themselves are never returned.",
responseObject = McpTokenResponse.class,
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = false,
authorized = {RoleType.User, RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin},
since = "24.0.0")
public class ListMcpTokensCmd extends BaseListCmd {
@Inject
private McpTokenService mcpTokenService;

@ACL
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = McpTokenResponse.class, description = "ID of the token.")
private Long id;

@Parameter(name = ApiConstants.USER_ID, type = CommandType.UUID, entityType = UserResponse.class,
description = "ID of the user that owns the tokens. Defaults to the calling user.")
private Long userId;

public Long getId() {
return id;
}

public Long getUserId() {
return userId;
}

@Override
public long getEntityOwnerId() {
return CallContext.current().getCallingAccountId();
}

@Override
public void execute() {
ListResponse<McpTokenResponse> response = mcpTokenService.listTokens(this);
response.setResponseName(getCommandName());
setResponseObject(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.mcp;

import javax.inject.Inject;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.ACL;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.McpTokenResponse;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.mcp.McpToken;
import org.apache.cloudstack.mcp.McpTokenService;

import com.cloud.user.Account;

@APICommand(name = "revokeMcpToken",
description = "Revokes an access token for the MCP (Model Context Protocol) server.",
responseObject = SuccessResponse.class,
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = false,
authorized = {RoleType.User, RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin},
since = "24.0.0")
public class RevokeMcpTokenCmd extends BaseCmd {
@Inject
private McpTokenService mcpTokenService;

@ACL
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = McpTokenResponse.class, required = true, description = "ID of the token to be revoked.")
private Long id;

public Long getId() {
return id;
}

@Override
public long getEntityOwnerId() {
McpToken token = mcpTokenService.findById(id);
if (token != null) {
return token.getAccountId();
}
return Account.ACCOUNT_ID_SYSTEM;
}

@Override
public void execute() {
if (!mcpTokenService.revokeToken(this)) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to revoke the MCP token.");
}
setResponseObject(new SuccessResponse(getCommandName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.response;

import java.util.Date;

import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.EntityReference;
import org.apache.cloudstack.mcp.McpToken;

import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;

@EntityReference(value = McpToken.class)
public class McpTokenResponse extends BaseResponse {
@SerializedName(ApiConstants.ID)
@Param(description = "ID of the MCP token.")
private String id;

@SerializedName(ApiConstants.NAME)
@Param(description = "Name of the MCP token.")
private String name;

@SerializedName(ApiConstants.DESCRIPTION)
@Param(description = "Description of the MCP token.")
private String description;

@SerializedName(ApiConstants.USER_ID)
@Param(description = "ID of the user that owns the MCP token.")
private String userId;

@SerializedName(ApiConstants.USERNAME)
@Param(description = "Username of the owner of the MCP token.")
private String username;

@SerializedName(ApiConstants.END_DATE)
@Param(description = "Expiration date of the MCP token.")
private Date endDate;

@SerializedName(ApiConstants.CREATED)
@Param(description = "Creation date of the MCP token.")
private Date created;

@SerializedName(ApiConstants.TOKEN)
@Param(description = "The MCP token. Only returned when the token is created.", isSensitive = true)
private String token;

public void setId(String id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setDescription(String description) {
this.description = description;
}

public void setUserId(String userId) {
this.userId = userId;
}

public void setUsername(String username) {
this.username = username;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}

public void setCreated(Date created) {
this.created = created;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}
}
36 changes: 36 additions & 0 deletions api/src/main/java/org/apache/cloudstack/mcp/McpToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.mcp;

import java.util.Date;

import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.api.Identity;
import org.apache.cloudstack.api.InternalIdentity;

/**
* Access token for the MCP (Model Context Protocol) server. Only the hash of the token is stored.
*/
public interface McpToken extends ControlledEntity, InternalIdentity, Identity {
long getUserId();

String getDescription();

Date getEndDate();

Date getCreated();
}
Loading